繁体   English   中英

JQuery.getJSON没有提醒json

[英]JQuery.getJSON not alerting json

我目前在localhost中运行:

$.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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM