Pro*_*eus 11 javascript jquery
在进行ajax调用时,请参阅下面的示例,success确实会重新调整201状态.你如何更好地处理这些成功函数中的200,201?
$.ajax({
type: "POST",
dataType: "json",
url: "http://api.domain.com/sms",
data: {
// Send value in mobile input field.
mobile: $("#mobile").val(),
},
// On successful AJAX call do the following.
success: function(data) {
$('#messageText').text('SMS successfully sent');
},
error: function(jqXhr) {
data = JSON.parse(jqXhr.responseText);
}
});
Run Code Online (Sandbox Code Playgroud)
acd*_*ior 13
使用statusCode对象:
var handle200 = function(data, textStatus, jqXHR) {
alert('200'); // success codes have the success signature
};
var handle201 = function(data, textStatus, jqXHR) {
alert('201'); // success codes have the success signature
// test it if you are in doubt:
console.log(data);
console.log(textStatus);
console.log(jqXHR);
};
var handle404 = function(jqXHR, textStatus, errorThrown) {
alert('404'); // failing codes have the error signature
});
var request = $.ajax({
type: 'POST',
url: '/myresource/posttarget',
data: { name: 'john' },
statusCode: {
200: handle200,
201: handle201,
404: handle404
}
});
Run Code Online (Sandbox Code Playgroud)
这是一个老问题,但无论如何我想发表评论.
我遇到了同样的问题,有一件事让我解决了这个问题,即"数据类型"未被设置.当你这样做时,jQuery将尝试猜测服务器返回的数据类型,并且当服务器返回没有内容的201时不会抛出错误.
希望能帮助到你.