Ajax甚至在成功时也没有给出响应

Ajax not giving response even on Success

本文关键字:响应 成功 Ajax      更新时间:2023-09-26

我有一个简单的页面

$.ajax({
    url: "abc.php",
    type: "POST",
    async: false,
    dataType: 'json',
    data:  { 'json': JSON.stringify(match) , 'source': source} ,
    success: function(response){
        alert("Alert 1");
    }
});

alert("hello");
location.reload();

它总是作为"hello"发出警报,我也尝试过fail:,但找不到合适的结果。请在这方面帮助我

问题是,一旦发送请求,就要重新加载页面,而无需等待ajax请求的响应返回。

解决方案是对ajax请求成功/完成回调进行重新加载

$.ajax({
    url: "abc.php",
    type: "POST",
    async: false,
    dataType: 'json',
    data:  { 'json': JSON.stringify(match) , 'source': source} ,
    success: function(response){
        alert("Alert 1");
    }
}).fail(function(xhr, status, error){
    alert('error:' + status + ':' + error+':'+xhr.responseText)
}).always(function(){
    location.reload();
});

如果这是你的完整代码,你就没有时间执行ajax调用abc.php。你只是在刷新页面,而不等待ajax响应。

从Jquery文档中,Ajax中的第一个字母代表"异步",这意味着操作是并行进行的,并且不能保证完成顺序