gpt4 book ai didi

javascript - 如何处理ajax 201

转载 作者:可可西里 更新时间:2023-11-01 02:18:34 25 4
gpt4 key购买 nike

当进行 ajax 调用时,请参见下面的示例 success 确实会返回 201 状态。你如何更好地处理这些,即 success 函数中的 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);
}
});

最佳答案

使用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
}
});

关于javascript - 如何处理ajax 201,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12410051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com