2017-05-31 57 views
0

我有这个简单的ajax调用来触发一个API来验证登录凭证。window.location行事怪异。它不重定向到指定的路径

$('#loginForm').submit(function (e) { 
    e.preventDefault(); 
    $.ajax({ 
     url: baseURL + "agent/login", 
     type: 'post', 
     data: {login: $('#login').val(), password: $('#password').val()}, 
     success: function (results) { 
      if(results.status === 0){ 
       window.location = baseURL + "agent/dashboard"; 
      } 
      else{ 
       alert(results.message); 
      } 
     }, 
     error: function(data) { 
      alert("Status: " + textStatus); alert("Error: " + errorThrown); 
     } 
    }); 
}); 

现在完整的目标网址是localhost/app/agent/loginbaseURL = http://localhost/app/

我正在服务器上接收请求。但是,当我回应背window.location不会重定向我localhost/app/agent/dashboard而它重定向到http://localhost/app/agent/localhost/app/agent/login

我不明白为什么会这样。这里是我的.htaccess如果它可以帮助

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)$ index.php/$1 [L] 
</IfModule> 

我使用笨3.1.4 XAMPP

+0

你确定'baseURL ==='http:// localhost/app /''?你有没有尝试只注销'baseURL'来确认它是什么? –

+1

如果您只是尝试在地址栏中输入URL“http:// localhost/app/agent/dashboard”,会发生什么? – CD001

+0

尽管所有的答案,它的解决问题的评论。原来在'dashboard'上有一个不好的重定向。有时候,这种简单的基本调试实践确实有帮助。谢谢! – eNeMetcH

回答

1

window.location是你不应该指定URL给它的对象。

欲了解更多信息,请here

的window.location的只读属性返回Location对象提供有关文档的当前位置信息 。

你应该做的是window.location.href = your_url

(模拟页面的URL链接上点击)

或使用window.location.replace(your_url)重定向到URL。

0

baseURL被解释为相对URL,而不是绝对URL。您应该指定完整的URL http://localhost/app/agent/login或截断baseURL/app/agent/login

1

使用window.location的像这样

// similar behavior as an HTTP redirect 
window.location.replace("http://stackoverflow.com"); 

// similar behavior as clicking on a link 
window.location.href = "http://stackoverflow.com";