There is a very disgusting problem in development today:
Commit with Ajax in jquery. Find the number of parameters in the assumed parameter. Background normal return 200 status code case 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. Another situation 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. Another similar approach to the previous one is to return JSON,JS to be the same configuration. There is no problem; a different point is found in the control code, which 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 change 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 fundamental problem is because the type of JS and the type of return are inconsistent.
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, one JSON format. Although it looks the same, it is actually different (because the string in Java is an object, not a native type, so there is a change in JSON, the detailed changes have no time to fine-grained. Remember first).
This point must be noted
3. Assume that Ajax requires a JSON string to be returned. Whatever the background processing type is, it's best to serialize it through JSON, even if it's a normal type
Conclusion:
Because the returned content is not in JSON format, it is in string format. The fundamental problem is because the type of JS and the type of return are inconsistent.
jquery ajax parameter DOT status 200 in error