jQuery的AJAX HTTP状态code [英] jquery ajax http status code
本文介绍了jQuery的AJAX HTTP状态code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我改变我的脚本的jQuery的时刻。这是我的旧的javascript:
I am changing my scripts to jquery at the moment. This is my old javascript:
var request = (window.XMLHttpRequest) ? new XMLHttpRequest() : (window.ActiveXObject ? new window.ActiveXObject("Microsoft.XMLHTTP") : false);
request.open("GET", url, true);
request.onreadystatechange = updatePage;
request.send(null);
function updatePage() {
if (request.readyState == 4) {
if (request.status == 200) {
var response = request.responseText;
doSomething();
} else if (request.status == 304) {
doSomethingElse();
} else {
}
}
}
我现在想改变这个jQuery的阿贾克斯:
I now want to change this to jquery-ajax:
request = $.ajax({
type: "GET",
url: url,
data: data,
success: updatePage
});
我怎么可以测试状态code(以及responseText的)请求返回,就像我在我的旧脚本做?
How can I test for the status code (and the responseText) returned by the request, like I did in my old script?
推荐答案
试试这个:
request = $.ajax({
type: "GET",
url: url,
data: data,
statusCode: {
200: function() {
doSomething();
},
304:function(){
doSomethingElse();
}
}
});
或
request = $.ajax({
type: "GET",
url: url,
data: data,
complete: function(e, xhr, settings){
if(e.status === 200){
}else if(e.status === 304){
}else{
}
}
});
这篇关于jQuery的AJAX HTTP状态code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文