js代码
$.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代码
header("content-type: text/html; charset=utf-8"); //这一句话,为的是utf8格式输出,有无这句话,都是一样执行ajax error分支
$menu=array("status"=>1);
exit(json_encode($menu));
去除js的datatype: "json",正常执行success分支;
使用jquery1.3.2 也正常执行success分支;但只要使用1.6,并且指定datatype:"json",立马执行error分支,报“parsererror,no conversion from text to json” json解析问题;
难道php的json_encode()函数,转换数组至json格式也会有问题?
Easily find JSON paths within JSON objects using our intuitive Json Path Finder
30
补充:
/test1.php
中的内容,就是上述php代码
因为你有 dataType: "json"
所以进入 ajax error分支 的前提是返回的数据不是 json 格式的
parsererror,No conversion from text to json (json转换失败)也佐证了这一点
为什么会转换失败呢?多半是你的 php 程序保存成了有 utf-8 BOM 头的格式了
这一点你可以通过:
2.php
<?phpecho bin2hex(file_get_contents('http://localhost/1.php')); 高手,佩服佩服!
给分了。
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号