我目前在本地主机上运行:JQuery.getJSON不报警JSON
$.getJSON("http://localhost/call", function(json) {
alert(json.something);
});
http://localhost/call
回报{something:1}
,但没有被提醒。
我目前在本地主机上运行:JQuery.getJSON不报警JSON
$.getJSON("http://localhost/call", function(json) {
alert(json.something);
});
http://localhost/call
回报{something:1}
,但没有被提醒。
{something:1}
心不是一个有效JSON字符串,但
{"something":1}
是。
如果更换
$.ajax({
url: 'http://localhost/call',
dataType: 'json',
success: function(){},
error: function(xhr, textStatus, errorThrown){
//you should get a parse error and end up here
}
});
你的电话,你应该在error
回调结束。
在你的PHP文件:
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
$arr = array('something' => 1, 'somethingelse' => 2);
echo json_encode($arr);
你JSON的反应似乎是无效的。而从jQuery 1.4开始,如果JSON文件包含语法错误,请求通常会自动失败 - http://api.jquery.com/jQuery.getJSON/ – Tom
您应该使用服务器端语言提供的方法将数据结构转换为JSON,而不是单独构建它。 *编辑:* http://php.net/manual/en/function.json-encode.php –