在进行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) 我使用API Rest.我想用AJAX和JQuery创建一个ressource .我的ressource是正确创建的,但调用了错误回调.
我的代码是:
$.ajax({
url: "/api/skills.json",
data: JSON.stringify(skill),
method: "POST",
contentType: "application/json",
statusCode: {
201: function (data) {
console.log("201");
}
},
success: function (data) {
console.log("success");
},
error: function (data) {
console.log("error");
},
complete: function (data) {
console.log("complete");
}
});
Run Code Online (Sandbox Code Playgroud)
从Firefox控制台导致"网络":
HTTP/1.1 201 Created
Date: Thu, 27 Oct 2016 08:29:08 GMT
Server: Apache
X-Powered-By: PHP/7.0.8
Cache-Control: no-cache
Location: http://localhost/api/skills/pdak12ada64d
Allow: POST
Content-Length: 0
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/json
Run Code Online (Sandbox Code Playgroud)
控制台的结果:
"201"
"error"
"complete"
Run Code Online (Sandbox Code Playgroud)
为什么JQuery调用201状态的错误回调?
我尝试使用快捷方法$.post …