简体   繁体   English

json_encode和getJSON

[英]json_encode and getJSON

I am using PHP at server side to get json formatted output as below. 我在服务器端使用PHP来获取json格式的输出,如下所示。 At client side I use jQuery to display the results but it displays null . 在客户端,我使用jQuery显示结果,但显示null Please let me know where I went wrong. 请让我知道我哪里出错了。 Any help is appreciated. 任何帮助表示赞赏。

PHP 的PHP

while ($r = mysql_fetch_assoc($result))
{
    $rows[] = $r;
}
echo json_encode($rows);

Output 产量

[
    {
        "a_name": "affles",
        "bname": "bua",
        "c_number": "10101010",
        "dateandtime": "2013-11-30 17:50:04"
    },
    {
        "a_name": "affles",
        "bname": "bua",
        "c_number": "10101010",
        "dateandtime": "2013-11-30 17:50:04"
    },
    {
        "a_name": "anan",
        "bname": "nesh",
        "c_number": "2017439441",
        "dateandtime": "2013-12-04 17:50:04"
    }
]

Client side 客户端

$.getJSON("http://apip.com/results.php", function (data) {
    $.each(data, function (index, value) {
        $('<li>' + value.a_name + '</li>').appendTo('#groups');
    });
});

Client side code using JSONP: I modified client side to use JSONP but still it returns null. 使用JSONP的客户端代码:我修改了客户端以使用JSONP,但仍然返回null。 An help is appreciatd 感谢您的帮助

<script>
(function() {
  var flickerAPI = "http://apip.com/results.php?jsoncallback=?";
  $.getJSON( flickerAPI, 
    (function( data ) {
      $.each( data.items, function( index, value ) {

$('<li>' + value.a_name + '</li>').appendTo('#groups');

      });
    });
})();
</script>

As you can see from this fiddle , it is working fine. 从这个小提琴中可以看到,它运行良好。 What I do suspect is going wrong is that you're using http://apip.com/results.php and you might be fooling jQuery into thinking you're doing a cross-domain request (which you might be doing), which is generally prohibited by browsers unless you use JSONP . 我怀疑是错的是您使用的是http://apip.com/results.php并且您可能在欺骗jQuery认为您正在执行跨域请求(您可能正在这样做),除非您使用JSONP否则通常被浏览器禁止。 If you own http://apip.com/ , use 如果您拥有http://apip.com/ ,请使用

$.getJSON("/results.php", function (data) {
    $.each(data, function (index, value) {
        $('<li>' + value.a_name + '</li>').appendTo('#groups');
    });
});

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

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