As I have declared success and error ajax options, where the response is 204, the ajax method goes to option success which leads to an error.
由于我已声明成功和错误ajax选项,其中响应为204,ajax方法转到选项成功,这会导致错误。
As per the documentation we can use, StatusCode or Complete methods but the disadvantage here is have to declare all the status code like 2?? series, 3?? series, 4?? series! As these response are dynamic and I am not sure about the http status code.
根据我们可以使用的文档,StatusCode或Complete方法,但这里的缺点是必须声明所有状态代码如2 ??系列,3 ??系列,4 ??系列!由于这些响应是动态的,我不确定http状态代码。
So, which is better way to handle http status code in jquery ajax?
那么,这是在jquery ajax中处理http状态代码的更好方法吗?
2 个解决方案
#1
14
The jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface. The third argument in the done function is a jqXHR object. This object has a property for the http status code of the result.
从jQuery 1.5开始,$ .ajax()返回的jqXHR对象实现了Promise接口。 done函数中的第三个参数是jqXHR对象。此对象具有结果的http状态代码的属性。
jqXHR.done(function(data, textStatus, jqXHR) {}); An alternative construct to the success callback option, the .done() method replaces the deprecated jqXHR.success() method. Refer to deferred.done() for implementation details. link
jqXHR.done(function(data,textStatus,jqXHR){});成功回调选项的替代构造,.done()方法替换了已弃用的jqXHR.success()方法。有关实现的详细信息,请参阅deferred.done()。链接
$.ajax({
url: "http://fiddle.jshell.net/favicon.png",
beforeSend: function ( xhr ) {
xhr.overrideMimeType("text/plain; charset=x-user-defined");
}
}).done(function ( data, textStatus, jqXHR) {
console.log(jqXHR.status); //handle your 204 or other status codes here
});
Fiddle http://jsfiddle.net/puleos/fVa7X/
Assuming you want to treat all non 200 status codes as an error you could do something like this:
假设您要将所有非200状态代码视为错误,您可以执行以下操作:
var p = $.ajax({
url: "http://fiddle.jshell.net/favicon.png",
beforeSend: function ( xhr ) {
xhr.overrideMimeType("text/plain; charset=x-user-defined");
}
});
p.done(function(data, textStatus, jqXHR) {
if(jqXHR.status !== 200) {
handleError(jqXHR.status);
return;
}
// Normal processing here
});
p.fail(function(jqXHR, textStatus) {
handleError(jqXHR.status);
});
#2
7
The solution above is a nice solution but for someone who already defines "success" and "error" on many components, this involves a lot of changes of code.
上面的解决方案是一个很好的解决方案,但对于已经在许多组件上定义“成功”和“错误”的人来说,这涉及到很多代码更改。
After a reading on the documentation, it is rather easy to get status code on success too :
阅读完文档后,很容易获得成功的状态代码:
jQuery.ajax({
..
'success' : function(data,textStatus,jqXHR) {
if (jqXHR.status == "204") {
}
},
error : function(jqXHR, exception) {
// Manage error
},
http://api.jquery.com/jquery.ajax/ => statusCode
http://api.jquery.com/jquery.ajax/ => statusCode
Then to check status code, a jqXHR.status will do the trick
然后检查状态代码,jqXHR.status就可以了
#1
14
The jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface. The third argument in the done function is a jqXHR object. This object has a property for the http status code of the result.
从jQuery 1.5开始,$ .ajax()返回的jqXHR对象实现了Promise接口。 done函数中的第三个参数是jqXHR对象。此对象具有结果的http状态代码的属性。
jqXHR.done(function(data, textStatus, jqXHR) {}); An alternative construct to the success callback option, the .done() method replaces the deprecated jqXHR.success() method. Refer to deferred.done() for implementation details. link
jqXHR.done(function(data,textStatus,jqXHR){});成功回调选项的替代构造,.done()方法替换了已弃用的jqXHR.success()方法。有关实现的详细信息,请参阅deferred.done()。链接
$.ajax({
url: "http://fiddle.jshell.net/favicon.png",
beforeSend: function ( xhr ) {
xhr.overrideMimeType("text/plain; charset=x-user-defined");
}
}).done(function ( data, textStatus, jqXHR) {
console.log(jqXHR.status); //handle your 204 or other status codes here
});
Fiddle http://jsfiddle.net/puleos/fVa7X/
Assuming you want to treat all non 200 status codes as an error you could do something like this:
假设您要将所有非200状态代码视为错误,您可以执行以下操作:
var p = $.ajax({
url: "http://fiddle.jshell.net/favicon.png",
beforeSend: function ( xhr ) {
xhr.overrideMimeType("text/plain; charset=x-user-defined");
}
});
p.done(function(data, textStatus, jqXHR) {
if(jqXHR.status !== 200) {
handleError(jqXHR.status);
return;
}
// Normal processing here
});
p.fail(function(jqXHR, textStatus) {
handleError(jqXHR.status);
});
#2
7
The solution above is a nice solution but for someone who already defines "success" and "error" on many components, this involves a lot of changes of code.
上面的解决方案是一个很好的解决方案,但对于已经在许多组件上定义“成功”和“错误”的人来说,这涉及到很多代码更改。
After a reading on the documentation, it is rather easy to get status code on success too :
阅读完文档后,很容易获得成功的状态代码:
jQuery.ajax({
..
'success' : function(data,textStatus,jqXHR) {
if (jqXHR.status == "204") {
}
},
error : function(jqXHR, exception) {
// Manage error
},
http://api.jquery.com/jquery.ajax/ => statusCode
http://api.jquery.com/jquery.ajax/ => statusCode
Then to check status code, a jqXHR.status will do the trick
然后检查状态代码,jqXHR.status就可以了