如何使用状态代码200,404,300(如何完成jquery并在内部失败) [英] how to use status codes 200, 404, 300 ( how jquery done and fail work internally)
本文介绍了如何使用状态代码200,404,300(如何完成jquery并在内部失败)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我通过使用jQuery学习了Ajax。我认为jQuery实现了使用状态代码,但对200,404和300等状态代码了解不多。
I have learnt Ajax by using jQuery. I think jQuery implements using status codes, but don't know much about status codes like 200, 404 and 300.
使用jQuery Ajax,其简单如下:
With jQuery Ajax, its simple as follows:
$.ajax({
url: "update.php",
type: "POST",
data: customObj
})
.done(function( data ) {
alert("data saved succesfully");
})
.fail(function( data ) {
alert( "failed to update data" );
});
有人可以解释如何使用这些状态代码200,404和300.
推荐答案
除了 @dfsq 写道,您可以处理特定的状态代码:
In addition to what @dfsq wrote, you can handle specific status codes:
$.ajax({
statusCode: {
404: function() {
alert( "page not found" );
}
}
});
或延期:
$.ajax({
url: "update.php",
type: "POST",
data: customObj
})
.fail(function( jqXHR, textStatus, errorThrown) {
if (jqXHR.status == 403) {
alert( "forbidden" );
}
});
或:
$.ajax({
url: "update.php",
type: "POST",
})
.statusCode({
401: function() { alert( 'Unauthorized' ); },
200: function() { alert( 'OK!'); }
});
这篇关于如何使用状态代码200,404,300(如何完成jquery并在内部失败)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文