Javascript JQuery.getJSON未向json发出警报

Javascript JQuery.getJSON未向json发出警报,javascript,jquery,json,Javascript,Jquery,Json,我当前正在本地主机中运行: $.getJSON("http://localhost/call", function(json) { alert(json.something); }); http://localhost/call返回{something:1},但未发出任何警报 {something:1} 但是,不是有效的字符串 {"something":1} 是 如果你把电话换成 $.ajax({ url: 'http://localhost/call', dataTy

我当前正在本地主机中运行:

$.getJSON("http://localhost/call", function(json) {
  alert(json.something);
});
http://localhost/call
返回
{something:1}
,但未发出任何警报

{something:1}
但是,不是有效的字符串

{"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
    }
});
您应该在
错误
回调中结束

在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文件包含语法错误,请求通常会以静默方式失败——您应该使用服务器端语言提供的方法将数据结构转换为JSON,而不是自己构建。编辑:狗狗,我就知道会是这样。@Babiker呵呵,我们都会犯错误;)您使用的服务器端语言是什么?@Babiker那么我建议使用
json\u encode()
function()。不要“手动”生成JSON,这只会让生活变得艰难;)当然,只是想用砂纸打磨掉锈迹,有一段时间没有做任何编码,我正在做一些快速测试:)@Babiker我添加了一些可能有用的php代码。但我还没试过。我已经有一段时间没有使用php了,但希望它能帮助我:)