2017-05-31 63 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";