jquery ajax json 怪异的问题

helloha2013 2015-11-01 10:07:37
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代码
<?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格式也会有问题?
...全文
108 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
helloha2013 2015-11-01
  • 打赏
  • 举报
回复
高手,佩服佩服! 给分了。
xuzuning 2015-11-01
  • 打赏
  • 举报
回复
因为你有 dataType: "json" 所以进入 ajax error分支 的前提是返回的数据不是 json 格式的 parsererror,No conversion from text to json (json转换失败)也佐证了这一点 为什么会转换失败呢?多半是你的 php 程序保存成了有 utf-8 BOM 头的格式了 这一点你可以通过: 2.php
<?php
echo bin2hex(file_get_contents('http://localhost/1.php'));
看到。输出结果中开始的 efbbbf 就是 BOM 头
helloha2013 2015-11-01
  • 打赏
  • 举报
回复
补充: /test1.php 中的内容,就是上述php代码

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧