javascript - JQuery.getJSON 不提醒 json

标签 javascript jquery 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);

关于javascript - JQuery.getJSON 不提醒 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14718365/

相关文章:

javascript - 在 express.js 源代码中传递参数的奇怪方式

php - Mysql 查询在编辑的 jTable 代码中不起作用,为什么?

javascript - 如何简单地使我的脚本可以访问我的 JSON 数据?

python - 如何将sqlite3数据转换为Json格式和CSV格式

javascript - 解析完整的 json 并搜索与值匹配的键

javascript - ComboBox 隐藏和显示 div

javascript - Vim + ALE + eslintrc。在子目录中

php - 修改 WordPress Jetpack 插件以使用 'Class' 而不是 'ID' 属性

jquery - 如何阻止过渡在图片库中跳动?

javascript - 如何将按钮点击转换为 onload 事件?