Bab*_*ker 3 javascript jquery json
我目前在localhost中运行:
$.getJSON("http://localhost/call", function(json) {
alert(json.something);
});
Run Code Online (Sandbox Code Playgroud)
http://localhost/call返回{something:1},但没有任何警报.
{something:1}
Run Code Online (Sandbox Code Playgroud)
但是,它不是有效的JSON字符串
{"something":1}
Run Code Online (Sandbox Code Playgroud)
是.
如果您更换电话
$.ajax({
url: 'http://localhost/call',
dataType: 'json',
success: function(){},
error: function(xhr, textStatus, errorThrown){
//you should get a parse error and end up here
}
});
Run Code Online (Sandbox Code Playgroud)
你最终应该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);
Run Code Online (Sandbox Code Playgroud)