The json format of jQuery1.4.2 prompts parsererror.

Source: Internet
Author: User
Tags parse error

Today, jquery is used to process ajax and json is used. But it is surprising that the error function is always called during jquery's ajax callback (A parsererror exception is always prompted), and the success function has not been executed at one time. The Code is as follows:

$. Ajax ({type: 'post', url: '/go. php? P = chat. chatCenter & a = send', ype: 'json', data: 'style = '+ Tstyle +' & content = '+ content, success: function (data) {$. chatCenter. sendEnd (data) ;}, error: function (data, t) {alert ("system exception [" + t + "]"); $. chatCenter. sendEnd ();}});

The server returns: {id: "1", data: "none "}

After a long time, I can switch the database back to 1.3.1 ~~ Lol, and finally found that it was caused by jQuery1.4.2. In 1.4.2, jq changed the JSON parsing from the original eval to an extremely strict $. parseJSON. What I found in the official instructions is as follows:

DataType
...
"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 .)

Http://api.jquery.com/jQuery.ajax/

That is to say, jQuery 1.4 imposes strict requirements on JSON data returned by the server and must comply with JSON standards.

Next, I will summarize the $. parseJSON instructions:

JQuery. parseJSON (json)

Passing in a malformed JSON string will result in an exception being
Thrown. For example, the following are all malformed JSON strings:

{Test: 1} (test does not have double quotes around it}
{'Test': 1} ('test' is using single quotes instead of double quotes ).
Additionally if you pass in nothing, an empty string, null, or
Undefined, 'null' will be returned from parseJSON. Where the browser
Provides a native implementation of JSON. parse, jQuery uses it to parse
The string. For details on the JSON format, see http://json.org /.

Now we have to pay attention to standards. Below are some examples that meet the standards:

{"Myvalue": 1}
{"Myvalue": "red "}
{"Myvalue": ["black", 250]}

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.