【问题标题】:jQuery does not return a JSON object to Firebug when JSON is generated by PHP当 PHP 生成 JSON 时,jQuery 不会向 Firebug 返回 JSON 对象
【发布时间】:2010-05-28 05:46:49
【问题描述】:

test.json的内容是:

{"foo": "The quick brown fox jumps over the lazy dog.","bar": "ABCDEFG","baz": [52, 97]}

当我使用以下 jQuery.ajax() 调用来处理 test.json 中的静态 JSON 时,

$.ajax({
    url: 'test.json',
    dataType: 'json',
    data: '',
    success: function(data) {
        $('.result').html('<p>' + data.foo + '</p>' + '<p>' + data.baz[1] + '</p>');
    }
});

我得到一个可以在 Firebug 中浏览的 JSON 对象。

但是,当使用相同的 ajax 调用时,URL 指向这个 php 脚本:

<?php
    $arrCoords = array('foo'=>'The quick brown fox jumps over the lazy dog.','bar'=>'ABCDEFG','baz'=>array(52,97));
    echo json_encode($arrCoords);
?>

打印这个相同的 JSON 对象:

{"foo":"The quick brown fox jumps over the lazy dog.","bar":"ABCDEFG","baz":[52,97]}

我在浏览器中得到了正确的输出,但 Firebug 只显示 HTML。在 Firebug 中展开 GET 请求时没有 JSON 选项卡。

【问题讨论】:

  • 查看响应标签

标签: php jquery json firebug


【解决方案1】:

试试

<?php
    header('Content-type: application/json');
    $arrCoords = array('foo'=>'The quick brown fox jumps over the lazy dog.','bar'=>'ABCDEFG','baz'=>array(52,97));
    echo json_encode($arrCoords);
?>

【讨论】:

    【解决方案2】:

    你最有可能需要设置 content-type 标题:

    // this is the "offical" content-type
    header('Content-Type: application/json');
    // or - this will probably work too
    header('Content-Type: text/javascript');
    

    【讨论】:

      猜你喜欢
      • 2018-08-11
      • 2011-08-17
      • 2013-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多