doter1995 2011-10-05 00:39
浏览 30
已采纳

使用PHP创建JSON提要以便与JQuery一起使用

Edit: Solved using the below suggestion of removing:echo '{"results":'.json_encode($arr).'}'; and replacing with echo json_encode($arr);

This combined with replacing these lines:

var items = [];
        $.each(data, function(key, val) {
            $.each(val, function(key, val)
            {
                    //Test insert into box using customer number
                $("#chatbox").html(val.number);
            });
        });

with

$.each(data, function(key, val) {
$("#chatbox").html(this.number);
});

Made it work, I can now access the object data using the this followed by the property I want.

End Edit

Hi could anyone help me with these JSON feed. I am almost there with it (I think) however whenever I put this feed into JQuery it is picked us as a string rather than as an array of JSON objects and I can't figure out why.

The PHP to create the feed:

<?php
session_start();

include 'connect.php';

    $number = $_SESSION['number'];

    $query = "SELECT * FROM $tableName WHERE number='$number'";

    $result = mysql_query($query, $sqlCon);

    $arr = array();
    while($obj = mysql_fetch_object($result)) {
        $arr[] = $obj;
    }

    if (count($arr) == 0)
    {
        echo "Your reference number is: $number";
    } else {
        echo '{"results":'.json_encode($arr).'}';
    }
?>

The returned JSON looks like this:

{"results":[{"id":"40","number":"466741","message":"dsfdsv","date":"2011-10-05","time":"00:28:32"},{"id":"41","number":"466741","message":"sacsac","date":"2011-10-05","time":"00:30:17"}]}

What I instead want is:

[{"id":"40","number":"466741","message":"dsfdsv","date":"2011-10-05","time":"00:28:32"},{"id":"41","number":"466741","message":"sacsac","date":"2011-10-05","time":"00:30:17"}]

Or a return value which would allow me to iterate over the objects.

The JQuery I'm reading it with:

$.getJSON('get.php', function(data) {

var items = [];
    $.each(data, function(key, val) {
        $.each(val, function(key, val)
        {
                //Test insert into box using customer number
            $("#chatbox").html(val.number);
        });
    });
});

I'm guessing my problem is the way in which I'm creating the JSON feed but I'm just stuck and cant figure out how to fix it.

Any help would be appreciated, thanks.

  • 写回答

1条回答 默认 最新

  • dongluanjie8678 2011-10-05 00:47
    关注

    You should set the headers from PHP to specify that it is json content. This isn't 100% required but good practice and you'll see the content-type parsed correctly in tools like firebug.

    header('Content-type: application/json');
    

    You are wrapping your json content with a results key that that you aren't looking for in your javascript so you either need to remove that part or look for that your javascript. Since you say you want the array and not the object with the results key/value just replace echo '{"results":'.json_encode($arr).'}'; with echo json_encode($arr);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决