Jqueryajaxjson's weird problem

Source: Internet
Author: User
Jqueryajaxjson: weird problem js code
$. Ajax ({
Type: "post ",
Url: "/test1.php ",
DataType: "json ",
Timeout: 16000,
Data: {cid: n },
Success: function (msg ){
If (1 = msg. status ){
Alert ('OK ');
} Else if (0 = msg. status ){
Alert ('sorry ')
}
},
Error: function (jqXHR, textStatus, errorThrown ){
Alert (textStatus + "," + errorThrown );
}
});

Php code
Header ("Content-type: text/html; charset = utf-8"); // This sentence is output in utf8 format. if it is used, the ajax error branch is executed in the same way.
$ Menu = array ("status" => 1 );
Exit (json_encode ($ menu ));

Remove the js dataType: "json" and execute the success branch normally;
If jquery1.3.2 is used, the success branch is also executed normally. If 1.6 is used and dataType: "json" is specified, the error branch is executed immediately and "parsererror, no conversion from text to json "json parsing problem;

Is it true that the json_encode () function of php may convert arrays to json format?


Reply to discussion (solution)

Supplement:
/Test1.php
Content is the above php code

Because you have dataType: "json"
Therefore, the prerequisite for entering the ajax error branch is that the returned data is not in json format.
Parsererror, No conversion from text to json (json conversion failed) also proves this point

Why does the conversion fail? Most of your php programs are saved in a format with the UTF-8 BOM header.
You can use:
2. php

 See. The efbbbf starting from the output result is the BOM header.

Master, admire!
Given.

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.