通过ajax将数组从PHP发送到javascript

时间:2012-03-07 17:18:25

标签: php javascript ajax json

我有这个PHP代码通过ajax将许多数组返回到javascript。

<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);

function get_events_data() {

    // load SimpleXML
    $nodes = new SimpleXMLElement('my_events.xml', null, true);

    $event_id = array();
    $channel_id = array();
    $channel_name = array();
    $channel_onclick = array();
    $event_site = array();
    $event_url = array();
    $start_date = array();
    $start_time = array();
    $end_date = array();
    $end_time = array();
    $event_notes = array();

    $n = 0;
    foreach($nodes as $node) 
    {       

        $event_id[$n] = $node['id'];               
        $channel_id[$n] = $node->channel['id'];
        $channel_name[$n] = $node->channel->name;
        $channel_onclick[$n] = $node->channel->onclick;
        $event_site[$n] = $node->event_site->name;
        $event_url[$n] = $node->event_site->url;
        $start_date[$n] = $node->start_date;
        $start_time[$n] = $node->start_time;
        $end_date[$n] = $node->end_date;
        $end_time[$n] = $node->end_time;
        $event_notes[$n] = $node->notes;
        $n++;
    }

    $return['event_id'] = $event_id;               
    $return['channel_id'] = $channel_id;
    $return['channel_name'] = $channel_name;
    $return['channel_onclick'] = $channel_onclick;
    $return['event_site'] = $event_site;
    $return['event_url'] = $event_url;
    $return['start_date'] = $start_date;
    $return['start_time'] = $start_time;
    $return['end_date'] = $end_date;
    $return['end_time'] = $end_time;
    $return['event_notes'] = $event_notes;

    echo json_encode($return);

}

echo get_events_data();

?>

在javascript方面,我有这个代码来访问数组。

    $.ajax({
        url: "get_events_data.php",
        type: "POST",
        dataType : 'json',
        data: { },
        cache: false,
        async: false,
        success: function (rdata) {
            var alert_data = 'event_id '+rdata.event_id[0]+'<br/>'+    
            'channel_id '+rdata.channel_id[0]+'<br/>'+
            'channel_name '+rdata.channel_name[0]+'<br/>'+
            'channel_onclick '+rdata.channel_onclick[0]+'<br/>'+
            'event_site '+rdata.event_site[0]+'<br/>'+
            'event_url '+rdata.event_url[0]+'<br/>'+
            'start_date '+rdata.start_date[0]+'<br/>'+
            'start_time '+rdata.start_time[0]+'<br/>'+
            'end_date '+rdata.end_date[0]+'<br/>'+
            'end_time '+rdata.end_time[0]+'<br/>'+
            'event_notes '+rdata.event_notes[0]+'<br/>';
            alert (alert_data);
        },
        error: function (request, status, error) {
            alert ("status "+status+" error "+error+"responseText "+request.responseText);
        },
});

当我在javascript端打印出每个数组的第一个元素时,它显示为“object”不显示该数组中的值。在javascript端访问数组数据的正确方法是什么?

4 个答案:

答案 0 :(得分:1)

在JavaScript中,关联数组几乎就是一个对象。您的脚本运行正常。对您的数据执行console.log()并探索它在控制台中的打印方式,然后使用它来访问数据。您还可以发布有问题的JSON数据,并可能获得更详细的答案。

答案 1 :(得分:1)

这个问题的答案告诉我如何做到这一点。 Reading a json encoded array in javascript

从PHP函数返回的json编码的rdata对象可以像这样rdata.event_id[i]["0"]读取。以下是rdata中对象的示例代码。

 var event = rdata.event_id[i]["0"]+' '+
                    rdata.channel_name[i]["0"]+' '+
                    rdata.channel_onclick[i]["0"]+' '+
                    rdata.event_site[i]["0"]+' '+
                    rdata.event_url[i]["0"]+' '+
                    rdata.event_onclick[i]["0"]+' '+
                    rdata.start_date[i]["0"]+' '+
                    rdata.start_time[i]["0"]+' '+
                    rdata.end_date[i]["0"]+' '+
                    rdata.end_time[i]["0"]+' '+
                    rdata.event_notes[i]["0"]+' ';
                    $('#event_list').append(event);

答案 2 :(得分:0)

使用此代替“类似”代码:

foreach($nodes as $node) 
{       
    $event_id[] = $node['id'];               
    $channel_id[] = $node->channel['id'];
    $channel_name[] = $node->channel->name;
    $channel_onclick[] = $node->channel->onclick;
    $event_site[] = $node->event_site->name;
    $event_url[] = $node->event_site->url;
    $start_date[] = $node->start_date;
    $start_time[] = $node->start_time;
    $end_date[] = $node->end_date;
    $end_time[] = $node->end_time;
    $event_notes[] = $node->notes;
}

答案 3 :(得分:-1)

您需要echo

echo json_encode($return);

echo get_events_data();

更好:

echo json_encode($return);
} // end of function

然后下一行

get_events_data();