Ajax executes the error callback instead of executing the success callback.

Source: Internet
Author: User
Tags parse error

The attached code is as follows:
JScript code:

Copy codeThe Code is as follows: $. ajax ({
Type: "post ",
Url: "jsp/loginManager. jsp ",
Data: "name =" + $ ('# rname '). attr ('value') + "& pwd =" + $ ('# pwd '). attr ('value '),
DataType: "text ",
Success: function (data ){
Alert (data );
}
});

Click Log on to successfully connect to the database and query the value (on the loginManager. jsp page, use System. out. print () to print the queried value ). But things in success are not executed. When a breakpoint is used, after the dataType is executed, success jumps out directly and the alert () in it is not executed. Why?
Changed the Code as follows:
JScript code:Copy codeThe Code is as follows: $. ajax ({
Type: "post ",
Url: "jsp/loginManager. jsp ",
Async: true,
Data: "name =" + $ ('# rname '). attr ('value') + "& pwd =" + $ ('# pwd '). attr ('value '),
DataType: "text ",
Success: function (data ){
Alert (data );
},
Error: function (e ){
Alert (e );
}
});

But it is still the same, no response, just refresh the login page !!!
The above problem is that the login page is refreshed. It is a small problem in link a. It turns out that I have href, start, href = "" In link a, so I refreshed the page, refresh the page and enter action, so the submitted data is also obtained, but the page is refreshed before it can be returned. Change to href = "#" and then OK! As:
<A href = '# 'onclick = 'delmenucontent (cellvalue)>
Another reason is that I am using asynchronous submission. When the verification is successful, the button submission event has been executed, so the page has been refreshed and changed to synchronous submission, the button submission event must be determined after ajax verification is completed to solve this problem!

I recently read jQuery's API documentation. When using jQuery's ajax, if dataType is specified as json, the success callback is not executed, but the error callback function is executed, which is extremely depressing. It can be executed later than version 1.2.6.

Then download several jquery versions, such as 1.3.2 and 1.4.0. If the specified ype is json, success callback cannot be executed. success callback can be executed only if the dataType is earlier than 1.3.

Finally go to the jquery site found the online api documentation to see the next, address: http://api.jquery.com/jQuery.ajax/, dataType found the following description
"Json": Evaluates the response as JSON and returns a JavaScript object. in jQuery 1.4 the JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. (See json.org for more information on proper JSON formatting .)

It turns out that jquery1.4 and later versions have very strict requirements on the json format. The success callback can be executed only when the format defined by the json.org website is met. Otherwise, errors will occur and the returned json data cannot be parsed. If it is more than 1.4, why cannot we execute the success callback when the ype specified in 1.3.2 is json?

No wonder, I returned an irregular string {success: true, id: 1} instead of a strict json format. It was changed to {"success": true, "id ": "1"} The success callback can be executed normally.
  
In JSON format, go to json.org to view details.
1) key name: enclosed in double quotation marks
2) string: enclosed in double quotation marks
3) numbers. Double quotation marks are not required for the boolean type.

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.