Add a number to the jquery ajax parameter. Status: 200: error, jqueryajax
I encountered a disgusting problem during development today:
When submitted using ajax in jquery, it is found that if the parameter contains a vertex, the error: function will be returned if the background returns a 200 status code normally.
1. Parameter data: "topicAbstractInfoVO. belongSubjectId =" + subjectId
2. Parameter data: "belongSubjectId =" + subjectId,
The request is returned normally. The status is 200. The first case jumps to the error processing block, and the second case 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 =;
Alert (B );
Alert (msg );
},
Error: function (msg ){
Var a = 1;
Var B =;
If (msg. responseText = "saveMarkSuccess "){
Alert (B );
}
Alert (msg );
}
});
Background processing method:
Public String addMarkDraft (){
Print ("saveMarkSuccess ");
Return "saveMarkSuccess ";
}
Troubleshooting:
1. I have encountered a situation similar to the jump to the error module when 200 is returned because dataType is set to html and the returned content is json. Type Mismatch also causes this problem. (This is not the reason for today's problem)
2. There is also a similar method that returns json. js is the same configuration and there is no problem. A difference found by comparing the code is the print () method.
2.1 The correct method is to return
Public String addMarkDraft (){
String te = JsonUtil. toJson ("saveMarkSuccess ");
Print (te );
Return "saveMarkSuccess ";
}
There are two ways to modify:
1. Change the dataType of ajax to text.
2. Convert the string "saveMarkSuccess" returned by the background into json
Conclusion:
Because the returned content is not in json format, it is in string format. The root problem is that the js type is inconsistent with the returned type.
Summary:
1. Even if a String is serialized in json format, it is still a String. The print log shows that the String is serialized in json format and remains unchanged.
2. Actually, the types returned to the foreground are different. One is plain text and the other is json format. Although it looks the same, it is actually different (because String is an object in java and is not a native type, it is changed after json processing, and there is no time to correct the specific changes, remember ). Pay attention to this 1.1
3. If ajax requires a json string to be returned, it is best to serialize it through json regardless of the background processing type.
Conclusion:
Because the returned content is not in json format, it is in string format. The root problem is that the js type is inconsistent with the returned type.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.