There is a very nasty problem in development today:
With the Ajax submission in jquery, found that if the parameters of a number of parameters, the background normal return 200 status code will also go to error:function.
1. Parameter data: "Topicabstractinfovo.belongsubjectid=" +subjectid
2. Parameter data: "Belongsubjectid=" +subjectid,
The request returns normally with a status of 200. In the first case, the error processing block is skipped, and the second case jumps to the success processing block.
The AJAX request code is as follows:
$.ajax ({
URL: "/topic/savemarkdraft.action",
DataType: "JSON",
Type: "POST",
Data: "Topicabstractinfovo.belongsubjectid=" +subjectid,
Async:true,
Success:function (msg) {
var a=2;
var b=a;
alert (b);
Alert (msg);
},
Error:function (msg) {
var a=1;
var b=a;
if (msg.responsetext== "Savemarksuccess") {
alert (b);
}
Alert (msg);
}
});
Background processing method:
Public String Addmarkdraft () {
Print ("savemarksuccess");
return "Savemarksuccess";
}
Troubleshoot issues:
1. Previously encountered a situation similar to return 200 when jumping to the error module, because datatype is set to HTML, the return content is JSON. Type mismatch can also cause this problem. (Today's problem is not this reason, excluded)
2. There is also a similar method in front of the return json,js is the same configuration, there is no problem; After comparing the code to find a different point, is the print () method
2.1 The correct method is to return this way
Public String Addmarkdraft () {
String te = Jsonutil.tojson ("savemarksuccess");
Print (TE);
return "Savemarksuccess";
}
There are two ways to modify this:
1. Change the datatype of Ajax to text
2. Turn the string "Savemarksuccess" returned in the background with JSON
Conclusion:
Because the returned content is not in JSON format, it is in string format. The underlying problem is the inconsistency between the type of JS and the type of return.
Summarize:
1. Although for a string, after being serialized with JSON or a string, it is not changed by the print log to discover that a string is serialized with JSON.
2. The type that is actually returned to the foreground is not the same, one is plain text, and a JSON format. Although it looks the same, it is actually different (because string in Java is an object, not a native type, so there is a change in JSON processing, the specific changes do not have time to fine-grained, first remember). This point must be noted
3. If Ajax requires a JSON string to be returned, no matter what the background processing type is, it is best to serialize it through JSON, even if it is a normal type
Conclusion:
Because the returned content is not in JSON format, it is in string format. The underlying problem is the inconsistency between the type of JS and the type of return.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
jquery ajax parameter DOT status 200 in error