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.