【发布时间】:2012-04-20 10:32:04
【问题描述】:
为了测试当我尝试加载不存在的资源(与我的网络服务器在同一主机上)时会发生什么,我设置了以下代码:
var wrongURL = "http://foo/bar.json"; // non-existent resource
$.ajax({
url: wrongURL,
dataType: 'json',
success: function(jsonResponse, textStatus, jqXHR) {
$.('#divOfInterest').html("you should never see this");
},
error: function(jqXHR, textStatus, errorThrown) {
$.('#divOfInterest').html("sorry, could not find URL");
}
});
// remainder of code...
我没有看到我的 div 显示消息 sorry, could not find URL,而是收到控制台错误:
GET http://foo/bar.json 404 (Not Found) - bar.json
error 调用中和$.ajax() 块之后的任何内容(即// remainder of code)都不会被执行
我的浏览器 (Safari 5.1.5) 似乎卡在 404 错误上并提前退出该功能。
如何优雅地处理错误并执行我的其余代码?
【问题讨论】:
-
它也在 safari 5.0.2 上运行
标签: jquery ajax http-status-code-404