在json中发送编码响应

时间:2011-09-03 17:52:51

标签: php javascript jquery json

我正在使用的项目中使用了很多jQuery。

我有一个javascript函数,它向一个以JSON返回数据的控制器发出ajax请求。

我想显示一条用户友好的消息,通知用户他/她尚未存储任何信息。但是我对如何在JSON中发送响应感到困惑,所以我的javascript函数可以确定用户是否有要显示的信息。

这是我的javascript函数:

function latest_pheeds() {
    var action = url+"pheeds/latest_pheeds";
    $('#pheed-stream').html('<div class="loading"></div>');
    $('.loading').append('<img src="'+pheed_loader_src+'" />');
    $.ajax({
        url:action,
        type:'GET',
        dataType:'json',
        error: function () {
        },
        success:function(data) {
            $('.loading').fadeOut('slow');
            $.each(data,function(index,item) {
            $('#pheed-stream').append
            (
            '<div class="pheed" id="'+item.pheed_id+'">'+
            '<p><a class="user_trigger" href="users/info/'+item.user_id+'">'
            +item.user_id+'</a></p>'+
            '<p>'+item.pheed+'</p>'+
            '<div class="pheed_meta">'+
            '<span>'+item.datetime+' Ago</span>'+
            '<span class="cm">'+item.comments+
            '<img class="comment_trigger" src="/pheedbak/assets/img/comment.png" title="Click to comment on pheed" onclick="retrieve_comments('+item.pheed_id+')">'+
            '</span>'+
            '<span>'+item.repheeds+
            ' Repheeds'+
            '<img class="repheed_trigger" src="/pheedbak/assets/img/communication.png" title="Click to repheed" onclick="repheed('+item.pheed_id+')">'+
            '</span>'+
            '<span>'+
            'Favourite'+
            '<img class="favourite_trigger" src="/pheedbak/assets/img/star.png" title="Click to make this a favourite" onclick="favourite_pheed('+item.pheed_id+')" />'+
            '</span>'+
            '</div>'+
            '</div>'
            );
        });
        }
    });
}

继续控制器功能ajax请求

function latest_pheeds() {
    //Confirm if a user is logged before allowing access
    if($this->isLogged() == true) {
        //load the pheed model for database interaction
        $this->load->model('pheed_model');
        //load user model
        $this->load->model('user_model');
        //load comment model
        $this->load->model('comment_model');
        //store the pheeds to a the $data variable
        $data = $this->pheed_model->get_latest_pheeds();
        //Load the date helper to calculate time difference between post time and current time
        $this->load->helper('date');
        //Current time(unix timetamp)
        $time = time();
        //pheeds
        $pheeds = array();
        if(count($data) > 0 ) {
            foreach($data as $pheed) {
                $row['pheed_id'] = $pheed->pheed_id;
                $row['user_id'] = $this->user_model->return_username($pheed->user_id);
                $row['pheed'] = $pheed->pheed;
                $row['datetime'] = timespan($pheed->datetime,$time);
                $row['comments'] = $this->comment_model->count_comments($pheed->pheed_id);
                $row['repheeds'] = $pheed->repheeds;
                $pheeds[] = $row;
            }

            echo json_encode($pheeds);
            $response['response'] = "Ok";
            $res[] = $response;
            echo json_encode($res)."\n";
        }
    } else {

    }

它生成JSON输出,但语法被破坏所以我无法用javascript读取它, 但是一旦我从上面的方法中删除了以下代码,它就能正常工作

  $response['response'] = "Ok";
  $res[] = $response;
  echo json_encode($res)."\n";

1 个答案:

答案 0 :(得分:14)

你必须在响应中只使用一次json_encode($ data),如果你想输出更多东西,你需要将你的数据合并到一个数组中并发送它。

编辑:要明确,你能做到的一种方法就是这样:

echo json_encode(array('pheeds' => $pheeds, 'res' => $res));

然后在JS中你会得到一个带有“pheeds”和“res”键的数组。

虽然它可能没什么实际意义,但我建议你在回显json编码的字符串之前这样做:

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