ajax-如何使用jQuery获取HTTP状态代码?
我想检查页面是否返回状态码401。这可能吗?
这是我的尝试,但只返回0。
$.ajax({
url: "http://my-ip/test/test.php",
data: {},
complete: function(xhr, statusText){
alert(xhr.status);
}
});
7个解决方案
89 votes
jQuery $.ajax()方法可以做到这一点
$.ajax(serverUrl, {
type: OutageViewModel.Id() == 0 ? "POST" : "PUT",
data: dataToSave,
statusCode: {
200: function (response) {
alert('1');
AfterSavedAll();
},
201: function (response) {
alert('1');
AfterSavedAll();
},
400: function (response) {
alert('1');
bootbox.alert('Error While Saving Outage Entry Please Check', function () { });
},
404: function (response) {
alert('1');
bootbox.alert('Error While Saving Outage Entry Please Check', function () { });
}
}, success: function () {
alert('1');
},
});
Ravi Mittal answered 2019-10-05T00:32:21Z
59 votes
第三个参数是XMLHttpRequest对象,因此您可以执行任何所需的操作。
$.ajax({
url : 'http://example.com',
type : 'post',
data : 'a=b'
}).done(function(data, statusText, xhr){
var status = xhr.status; //200
var head = xhr.getAllResponseHeaders(); //Detail header info
});
b123400 answered 2019-10-05T00:32:45Z
20 votes
使用错误回调。
例如:
jQuery.ajax({'url': '/this_is_not_found', data: {}, error: function(xhr, status) {
alert(xhr.status); }
});
会提示404
baloo answered 2019-10-05T00:33:16Z
7 votes
我找到了您可以轻松解决的解决方案, 使用状态代码检查服务器响应代码。
示例:
$.ajax({
type : "POST",
url : "/package/callApi/createUser",
data : JSON.stringify(data),
contentType: "application/json; charset=UTF-8",
success: function (response) {
alert("Account created");
},
statusCode: {
403: function() {
// Only if your server returns a 403 status code can it come in this block. :-)
alert("Username already exist");
}
},
error: function (e) {
alert("Server error - " + e);
}
});
Jigar Trivedi answered 2019-10-05T00:33:42Z
7 votes
我认为您还应该实现$ .ajax方法的错误功能。
错误(XMLHttpRequest,textStatus, errorThrown)功能
请求时要调用的函数 失败。 函数传递三 参数:XMLHttpRequest对象, 描述错误类型的字符串 发生和可选 异常对象(如果发生)。 第二个可能的值 参数(除null外)为“超时”, “错误”,“未修改”和 “ parsererror”。
$.ajax({
url: "http://my-ip/test/test.php",
data: {},
complete: function(xhr, statusText){
alert(xhr.status);
},
error: function(xhr, statusText, err){
alert("Error:" + xhr.status);
}
});
GvS answered 2019-10-05T00:34:19Z
6 votes
$.ajax({
url: "http://my-ip/test/test.php",
data: {},
error: function(xhr, statusText, errorThrown){alert(xhr.status);}
});
vartec answered 2019-10-05T00:34:36Z
3 votes
我将jQuery Ajax封装为一个方法:
var http_util = function (type, url, params, success_handler, error_handler, base_url) {
if(base_url) {
url = base_url + url;
}
var success = arguments[3]?arguments[3]:function(){};
var error = arguments[4]?arguments[4]:function(){};
$.ajax({
type: type,
url: url,
dataType: 'json',
data: params,
success: function (data, textStatus, xhr) {
if(textStatus === 'success'){
success(xhr.code, data); // there returns the status code
}
},
error: function (xhr, error_text, statusText) {
error(xhr.code, xhr); // there returns the status code
}
})
}
用法:
http_util('get', 'http://localhost:8000/user/list/', null, function (status_code, data) {
console(status_code, data)
}, function(status_code, err){
console(status_code, err)
})
aircraft answered 2019-10-05T00:35:01Z