jQuery ajax 请求错误回调
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12073720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery ajax request error callback
提问by Albert
I'm testing the error callback on a jQuery ajax request and I've got a question. Below is my ajax request
我正在测试 jQuery ajax 请求的错误回调,我有一个问题。下面是我的ajax请求
$.ajax({
type:'get',
url:'non-existant-file.php',
data: { newFile : $('#name').val() },
beforeSend: function() {
// make nice image to show the request is running
},
success: function() {
// show results of successfull request
},
error: function(jq,status,message) {
alert('A jQuery error has occurred. Status: ' + status + ' - Message: ' + message);
}
});
The file does not exist, for the sole purpose of testing the error that is thrown. I would expect that it returns a 404 as status, and 'File not found' as message. Atleast, that's how I understand it when I read the docs:
该文件不存在,其唯一目的是测试抛出的错误。我希望它返回 404 作为状态,并返回“找不到文件”作为消息。至少,这就是我阅读文档时的理解:
Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn.
第二个参数(除了 null)的可能值是“超时”、“错误”、“中止”和“解析器错误”。当发生 HTTP 错误时,errorThrown 会接收 HTTP 状态的文本部分,例如“未找到”或“内部服务器错误”。从 jQuery 1.5 开始,错误设置可以接受函数数组。每个函数都会被依次调用。
But, the alert I get is nothing like what I expected:
但是,我得到的警报与我的预期完全不同:
A jQuery error has occurred. Status: error - Message: undefined
Am I understanding the docs wrong or am I just not doing it right?
我对文档的理解是错误的还是我做的不对?
采纳答案by Royce Feng
I believe it has to do with how your routing server responds to accessing a route that does not exist. JSFiddle, for instance, will return a slightly more helpful message: http://jsfiddle.net/dxSma/
我相信这与您的路由服务器如何响应访问不存在的路由有关。例如,JSFiddle 会返回一条更有用的消息:http: //jsfiddle.net/dxSma/
回答by sQVe
I use the following for error handling with jQuery AJAX:
我使用以下内容与 jQuery AJAX 进行错误处理:
error : function( xhr ) {
var readyState = {
1: "Loading",
2: "Loaded",
3: "Interactive",
4: "Complete"
};
if(xhr.readyState !== 0 && xhr.status !== 0 && xhr.responseText !== undefined) {
alert("readyState: " + readyState[xhr.readyState] + "\n status: " + xhr.status + "\n\n responseText: " + xhr.responseText);
}
}
Maybe this can give you a couple of hints on how to get it to work properly.
也许这可以为您提供有关如何使其正常工作的一些提示。
回答by Rajat Modi
you can also work with the "Global Ajax Event Handlers"
您还可以使用“全局 Ajax 事件处理程序”
.ajaxError() I am here show you the example and more you can find on this link.
.ajaxError() 我在这里向您展示示例以及您可以在此链接上找到的更多信息。
http://api.jquery.com/ajaxError/
http://api.jquery.com/ajaxError/
Show a message when an Ajax request fails.
当 Ajax 请求失败时显示一条消息。
jQuery("#msg").ajaxError(function(event, request, settings){
jQuery(this).append("<li>Error requesting page " + settings.url + "</li>");
});
or you can also work it like that an OLD way :)
或者你也可以像旧的方式那样工作:)
jQuery.ajax({
...,
complete: function(e, XHR, options) {
if (XHR.status == 200) { // success
} elseif (XHR.status == 500) { // Internal Server Error
} elseif (XHR.status == 404) { // File Not found
}
});