當用戶在我的應用程序上按下'Process'按鈕時,我希望應用程序觸發AJAX請求,然後立即將用戶重定向到另一個屏幕而無需等待AJAX的結果請求。我相信我已經編寫了適當的代碼,但我注意到屏幕正在等待AJAX在重定向之前完成。我錯過了什麼?Javascript不會異步超越AJAX調用
$('#process-btn').on('click', function()
{
// disable the process & cancel buttons to prevent
// double submission or interruption
$('#cancel-btn').addClass('disabled');
$(this).addClass('disabled');
// trigger the AJAX require to process the uploaded file on the server side
$.ajax({
url: $('#form').attr('action'),
type: 'post',
data: $('#form').serialize(),
success: function() {
//on success
}
});
// redirect the user to view list
// this line is not being called immediately -
// this is being called only after AJAX returns
window.location.replace(www_root + 'Home/index');
});
您是否在ajax設置中設置了'async:false' ...嘗試在ajax選項中設置'async:true'並參見 – 2014-12-19 04:07:01
爲什麼不包含'window.location.replace(www_root +'Home/index ');'在ajax成功函數中?既然你想根據成功結果重定向。 – Aditya 2014-12-19 04:08:12
如果您希望在成功之前離開頁面,那麼擁有「成功」塊有什麼意義? – Verhaeren 2014-12-19 04:08:15