Home  >  Article  >  Web Front-end  >  Jquery ajax cannot parse json objects and reports Invalid JSON error reasons and solutions_jquery

Jquery ajax cannot parse json objects and reports Invalid JSON error reasons and solutions_jquery

WBOY
WBOYOriginal
2016-05-16 18:31:281189browse

Let’s talk about the recent problems encountered when using the $.ajax() method to parse json objects in jquery 1.4.

Json object is:

Copy code The code is as follows:

[{ name:'Second-hand house for sale',infoCount:0,pageUrl:'/ershoufang'},{name:'Second-hand house for purchase',infoCount:0,pageUrl:'/qiugou'},{name:'Second-hand house decoration', infoCount:0,pageUrl:'/esfzhuangxiu'},{name:'Second-hand recycling',infoCount:0,pageUrl:'/huishou'},{name:'Second-hand motorcycle',infoCount:0,pageUrl:'/motor '},{name:'Second-hand car purchase',infoCount:0,pageUrl:'/ershoucheqg'},{name:'Second-hand car transfer',infoCount:9,pageUrl:'/ershouche'},{name:'Second-hand car Market',infoCount:0,pageUrl:'/ershoushichang'}]


I don’t know if anyone has noticed the problem with this json object. This way of writing is no problem in js scripts and versions before jquery 1.4. Because in 1.3 and earlier versions, jQuery parses json objects through javascript's eval method. In 1.4, jQuery uses a stricter approach to parsing json. All content must use double quotes.

This can be seen from jQuery.parseJSON(json). The explanation of parseJSON in the document is:

Accepts a JSON string and returns the parsed object.

Passing in a malformed JSON string will throw an exception. For example, the following are malformed JSON strings:
{test: 1} (test is not surrounded by double quotes)
{'test': 1} (single quotes are used instead of double quotes)

In addition, if you pass in nothing, or an empty string, null or undefined, parseJSON will return null.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn