Today, we found that in the case of the return value correct, ajax take error method, did not take the success method, using the following method self-test
原代码:
$.ajax({
url: environment.serverUrl + '/questionClassify/modify',
type: 'put',
data: JSON.stringify(body),
async: false,
// 返回的数据类型是json
dataType: 'application/json',
contentType: 'application/json;charset=UTF-8',
success: function (data) {
alert(data.message);
},
error: function (data) {
alert(data.responseText);
}
});
Modify the code self-test:
error: function (the XMLHttpRequest, textStatus, errorThrown) {
Alert (XMLHttpRequest.status);
Alert (the XMLHttpRequest.readyState);
Alert (textStatus);
}
operation result:
alert (XMLHttpRequest.status); // pop 200 normal
alert (XMLHttpRequest.readyState); // pop 4 normal
alert (textStatus); // pop parsererror error
alert (XMLHttpRequest.status); // pop 200 normal alert (XMLHttpRequest. readyState); // 4 normal pop-up alert (textStatus); // error pop parsererror
parsererror error return type error, in the source code dataType: 'application / json', I specified json return value, so that the rear end of the string must be returned json standard format, else error jquery1.4 + versions of the above , because it is not generating the object with the eval, with the JSON.parse, if the string is not the standard error is reported.
Solution:
First dataType: 'application / json', commented
in Success: function (Data, textStatus) {
the console.log (Data);
observe whether the string display format json
Finally we found the value returned is indeed a json format, but not the standard json format, all the error, the next level is too low, are not converted to a standard format, all I'll dataType: 'application / json', completely commented out, do not specify the return type.
---------------------
Disclaimer: This article is CSDN blogger "Guo full of bright -Felix 'original article, follow the CC 4.0 by-sa copyright agreements, please attach a reprint the original source link and this statement.
Original link: https: //blog.csdn.net/G165945348/article/details/85226142
View jquery.form.min.js source informed that there are three methods
options.success = function(data, status, xhr) { // jQuery 1.4+ passes xhr as 3rd arg var context = options.context || this ; // jQuery 1.4+ supports scope context for (var i=0, max=callbacks.length; i < max; i++) { callbacks[i].apply(context, [data, status, xhr || $form, $form]); } }; if (options.error) { var oldError = options.error; options.error = function(xhr, status, error) { var context = options.context || this; oldError.apply(context, [xhr, status, error, $form]); }; } if (options.complete) { var oldComplete = options.complete; options.complete = function(xhr, status) { var context = options.context || this; oldComplete.apply(context, [xhr, status, $form]); }; }
So the application complete function to do a callback function, if the background is returned instead of the standard json format Status value will be parsererror
function DeleteForm () {
$ ( '# addExcelForm') {
url: "",
of the type: "POST",
timeout: 10000 ,
dataType: 'JSON',
Complete: functioin (XHR, the Status) {
IF (Stauts == 'Success') {
var respMessage = xhr.responseText;
IF (! respMessage.indexOf ( "the Message") = -. 1) {
var .parseJSON $ = Message (respMessage)
IF (message.message = "successful upload") {Alert (message.message + message.number)}
}
}
}
}
}
// the responseText is backstage pass json data, you can parse out to do the operation.