【发布时间】:2013-11-09 03:46:51
【问题描述】:
所以在某些计算机上一切正常。我打开我的笔记本电脑 > Firefox > 我的网站。哦,不,ajax 请求返回错误'[object Object]'。我进入 chrome 和 IE .....JSON 字符串在警报中返回“joe is the man from: JSON”就好了。我需要知道 Jquery 是否存在处理 JSON 的已知问题?考虑到我手机的 chrome 返回错误“[object Object]”,我不能让人们在访问我的页面时遇到这个随机问题。
<?php
//------------------------------The php file sending JSON
$results = array(
"price" => "joe is the man from: JSON",
);
print(json_encode($results));
?>
<script>
$.ajax({
url: "myphpfile.php",
dataType: "json",
success: function(data){
alert(data.price);
},
error: function(error){
alert(error);
}
});
}
</script>
我还尝试将其添加到 PHP 文件中以防万一......结果相同:
header('Content-type: application/json');
好的,我把它改成这样:
$.ajax({
url: "myphpfile.php",
dataType: "json",
success: function(data){
alert(data.price);
},
error: function(xhr, status, error) {
alert(status);
alert(xhr.responseText);
alert(xhr);
}
});
警报(状态) - 返回“错误” 警报(xhr.responseText); - 返回一个空白框 警报(xhr); - 返回'[object Object]'
编辑:这已停止在所有浏览器上运行....
【问题讨论】:
-
所有代码都在一个文件中吗?如果是这种情况,那么您的
<script>标记将显示为 json 输出的一部分,FF 将正确拒绝。 ajax 调用的 json 响应应该只包含 json 文本,而不包含其他内容。 -
不,你们只能这样看
-
function( xhr,status,message) { alert(message) }会提供更多信息 -
"我需要知道 Jquery 是否存在处理 JSON 的已知问题?"没有。
-
你检查过firebug中的网络标签吗?错误是否在发出请求之前发生?如果不是,您可以查看网络请求的原始响应文本吗?如果是这样,如果将其作为字符串粘贴到 Firebug 控制台并 JSON.parse() 会发生什么?
标签: javascript php jquery json google-chrome