我的代碼是:如何調試jQuery Ajax請求?
var test = "it isn't working";
var response = $.ajax({
type: 'GET',
url: 'jquerydemo.php', //This is in the same site as the page calling this request, so it's not a same-domain error.
success: function(){
test = "it's working";
},
error: function(){
alert("Error detected");
}
}).responseText;
alert(test);
我測試的狀態碼,它出來的200和誤差函數從未熄滅,但同樣沒有成功的功能。就像我在評論中所說的那樣,這不是一個同源的政策錯誤。它只是說「它不工作」。這裏發生了什麼?
您也可以選擇添加'異步的選項:TRUE'你在你的'ajax'呼叫傳遞的設置,但要記住,這將停止執行的是整個線程在你的腳本中,直到請求返回。 – dfreeman
@SchiefGelaufen - 我假設你的意思是'async:false'。將ajax調用轉換爲同步調用確實是一種選擇,但由於它在ajax調用期間鎖定了整個網頁,因此它幾乎不是最好的設計選擇。使用'async:true'編寫代碼會更加複雜,但可以提供更好的用戶體驗。 – jfriend00
哎呦,我這個傻傻的錯字。是的,'async:false'。做一個同步ajax請求並不一定是正確的(至少,有很多情況下它不會),但在你的成功中放置任何依賴於響應的邏輯當然被認爲是更好的做法回電話。 – dfreeman