我目前在本地主机上运行:
$.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);
关于javascript - JQuery.getJSON 不提醒 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14718365/