Add a number to the jquery ajax parameter. Status: 200: error, jqueryajax

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.