Php 在收到成功回调之前重定向用户
这是我的密码:Php 在收到成功回调之前重定向用户,php,jquery,ajax,asynchronous,Php,Jquery,Ajax,Asynchronous,这是我的密码: $.ajax( { type: "POST", url: "/script.php", data: { "action" : "importData" }, cache: false, async: true, success: function() { window.location.href = "/next-page"; } } ); 当用户登陆此页面时,ajax将触发php脚本,完成该脚本大约需要1
$.ajax( {
type: "POST",
url: "/script.php",
data: { "action" : "importData" },
cache: false,
async: true,
success: function() {
window.location.href = "/next-page";
}
} );
当用户登陆此页面时,ajax将触发php脚本,完成该脚本大约需要15秒。我不想让用户等待脚本完成然后重定向它们,而是希望得到某种类型的确认,确认脚本已被触发,然后在不等待整个脚本完成的情况下重定向用户。该脚本的结果直到几分钟后才在工作流中使用
这可能吗?如何执行此操作?$.ajax()
返回XMLHttpRequest
实例:
var xhr = $.ajax({
type: 'POST',
url: '/script.php',
data: {action: 'importData'},
cache: false,
success: function(){
location = '/next-page';
}
});
/* to test if problem is jQuery remove this line and take comments out
var px = xhr.onreadystatechange;
*/
xhr.onreadystatechange = function(){
//if(px)px();
if(xhr.readyState === 3 && xhr.status === 200){ // look at Properties -> https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
// do stuff here with xhr.readyState less than 4
}
}
可以找到有效的XMLHttpRequest.readyState
属性值
请注意,如果重定向用户,当前页面上的JavaScript将停止。用户重定向到的页面可能有自己的JavaScript 默认情况下,它不会等待,只需在调用(从成功中删除)xhr.onreadystatechange未被调用后将重定向移动到,因此my window.location.href=“/next page”;在该if语句中,如果未执行?
xhr。在您的情况下,XMLHttpRequest
,xhr
的特定实例的readyState
更改时,将自动调用onreadystatechange
。success
当xhr.readyState==4&&xhr.status==200)
时,$.ajax({})
中的success
方法触发。如果success
没有执行,可能是因为发生在xhr.onreadystatechange
函数中的代码正在做一些事情来阻止它,或者jQuery
使用了与我相同的赋值方法,而不是xhr.addEventListener
。如果这是一个jQuery
问题,我无论如何都有解决办法。