Question about jQuery dataType specified as json

Source: Internet
Author: User
Tags parse error

Drowning fish

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.

 

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.