看起来 jQuery 的 .ajax
函数会触发 'success'
函数,即使它没有从服务器得到响应。我认为这是一个错误。我通过检查 'request.status===0'
是否存在需要检查 'success'
函数的其他实例来检测到此事件即使出现错误情况也会被解雇吗?
我已经在下面发布了我现在使用的代码。这种情况通常如何处理?
$.ajax({
async : true, cache : false, dataType : 'json',
type : 'POST',
url : 'person/' + personKey,
data : JSON.stringify({firstName:'Jilopi',}),
success : function(data, textStatus, request){
if( request.status === 0 ){
alert('Error: problem saving person - no response');
}else{
alert('Success: saved person');
}
},
error : function(request, textStatus, errorThrown){
alert('Error: problem saving person');
},
});
最佳答案
您已经发现了我所知道的唯一“错误成功”问题。详细说明一下:这是严格针对 Opera 的解决方法(它返回实际上 304
作为 0
) ,您可以在此处查看 1.4.2(及之前版本)中的评论:http://github.com/jquery/jquery/blob/1.4.2/src/ajax.js#L551
但是,it has been removed in jQuery 1.4.3 because of your exact issue ,你可以看到 the change that did it here .
简短回答:升级到 jQuery 1.4.3+要解决您的问题,您可以取消 status === 0
检查。
关于jquery - 即使服务器没有响应,jQuery 中的 "ajax"调用也会触发 "success"函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3969646/