Javascript 如何获取jquery ajax状态码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7078978/
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
how to get jquery ajax status code
提问by Shades88
I want to know that how can we get ajax status code in jquery. I have this ajax block:
我想知道我们如何在 jquery 中获取 ajax 状态代码。我有这个 ajax 块:
$.ajax{
type: "GET",
url: "keyword_mapping.html",
data:"ajax=yes&sf="+status_flag,
success: callback.success,
complete: rollup_filters(),
failure: function(){
alert("Failure");
}
}
Now in above code, in case of failure, how can i get ajax status code and some description of that status code ??
现在在上面的代码中,如果失败,我如何获得 ajax 状态代码和该状态代码的一些描述?
回答by Justin Ethier
You want to use the error
option to capture this. For example:
您想使用该error
选项来捕获它。例如:
error: function (jqXHR, textStatus, errorThrown)
// Your handler here...
}
You can then use the jqXHR
object to retrieve information about the failure.
然后,您可以使用该jqXHR
对象来检索有关失败的信息。
从文档:
For backward compatibility with XMLHttpRequest, a jqXHR object will expose the following properties and methods:
- readyState
- status
- statusText
- responseXML and/or responseText when the underlying request responded with xml and/or text, respectively
- setRequestHeader(name, value) which departs from the standard by replacing the old value with the new one rather than concatenating the new value to the old one
- getAllResponseHeaders()
- getResponseHeader()
- abort()
为了与 XMLHttpRequest 向后兼容,jqXHR 对象将公开以下属性和方法:
- 就绪状态
- 地位
- 状态文本
- responseXML 和/或 responseText 当底层请求分别用 xml 和/或文本响应时
- setRequestHeader(name, value) 通过用新值替换旧值而不是将新值连接到旧值而偏离标准
- getAllResponseHeaders()
- getResponseHeader()
- 中止()
回答by Brad Christie
First, you have a few syntax errors. The above is a method call, so it needs to follow $.ajax({ ... });
(with parenthesis).
首先,您有一些语法错误。上面是方法调用,所以需要跟$.ajax({ ... });
在后面(带括号)。
Secondly, you want to supply the errorproperty as part of the object, not failure (see docsfor more information).
其次,您希望将错误属性作为对象的一部分提供,而不是失败(有关更多信息,请参阅文档)。
Third, when you do bind to an error, you are supplied three parameters: jqHXR, textState, errorThrow. These arguments will supply you the details of a failed AJAX call. (More specifically, try jqXHR.status
)
第三,当你绑定到一个错误时,你会被提供三个参数:jqHXR、textState、errorThrow。这些参数将为您提供失败的 AJAX 调用的详细信息。(更具体地说,尝试jqXHR.status
)
Alternatively, you can bind to the $.ajaxError
function as well.
或者,您也可以绑定到该$.ajaxError
函数。
UpdateTo keep this more up-to-date, you should now be following the Deferred
API (as of jQuery 1.5), which would make binding to an error look something like the following:
更新为了保持最新状态,您现在应该遵循Deferred
API(从 jQuery 1.5 开始),这将使绑定到错误看起来像以下内容:
$.ajax({ /* options */ })
.done(function( data, textStatus, jqXHR ){
// here you bind to a successful execution.
.fail(function( jqXHR, textStatus, errorThrown ){
// Here you can catch if something went wrong with the AJAX call.
})
.always(function(){
// here you can execute code after both (or either) of
// the above callbacks have executed.
});
回答by Peter Hilton
Change your failure callback to
将您的失败回调更改为
error:function (xhr, options, error){
alert(xhr.status);
alert(error);
}
回答by ShankarSangoli
There is nothing like failure
in ajax settings. Replace failure by error and you get 3 arguments in the error callback. First argument is the xhr object which has a status property in it.
failure
在 ajax 设置中没有任何东西。用错误替换失败,你会在错误回调中得到 3 个参数。第一个参数是 xhr 对象,其中有一个 status 属性。
$.ajax{
type: "GET",
url: "keyword_mapping.html",
data:"ajax=yes&sf="+status_flag,
success: callback.success;
complete: rollup_filters(),
error: function(jqXHR, textStatus, errorThrown){
alert(jqXHR.status);
}
}