Jquery. ajax cannot parse json objects. The cause and solution of Invalid JSON error are reported.

Source: Internet
Author: User
Tags parse error javascript eval

We know that the JSON object caused by the Invalid json error cannot be parsed. Generally, the syntax of the json string returned by the server is incorrect. In this case, we only need to carefully check the json to solve the problem.

Next let's talk about the problems encountered recently when using the $. ajax () method in jquery 1.4 to parse json objects.

The Json object is:

[

{Name: 'Sale of second-hand houses ', infoCount: 0, pageUrl:'/ershoufang '},

{Name: 'second-hand house purchase ', infoCount: 0, pageUrl:'/qiugou '},

{Name: 'second-hand house decoration ', infoCount: 0, pageUrl:'/esfzhuangxiu '},

{Name: 'secondhand recycling', infoCount: 0, pageUrl: '/huishou '},

{Name: 'Used motorcycle ', infoCount: 0, pageUrl:'/motor '},

{Name: 'Used car purchase ', infoCount: 0, pageUrl:'/ershoucheqg '},

{Name: 'second-hand car transfer', infoCount: 9, pageUrl: '/ershoe '},

{Name: 'secondhand market ', infoCount: 0, pageUrl:'/ershoushichang '}

]

I wonder if you have found any problems with this json object. This method is correct in js scripts and versions earlier than jquery 1.4. In versions 1.3 and earlier, jQuery parses json objects using the javascript eval method. In section 1.4, jQuery uses a more rigorous method to parse json. All content must use double quotation marks.

As shown in jQuery. parseJSON (json), parseJSON is interpreted as follows:

Accepts a JSON string and returns the parsed object.

Inputting a malformed JSON string throws an exception. For example, the following are malformed JSON strings:
{Test: 1} (test is not surrounded by double quotation marks)
{'Test': 1} (using single quotes instead of double quotes)

In addition, if you do not input anything, or an empty string, null, or undefined, parseJSON will return null.


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.