json编码返回非json对象

时间:2013-10-08 20:08:05

标签: php jquery ajax json codeigniter

我使用$ .ajax检查控制器上的数据。如果在我为json编码放入数组的变量之前没有进程,它工作正常。 控制器回显数据,但视图不会将其呈现为json编码数据。 控制器:

public function redeem($item){
    $this->load->model('point_model');
    $cost = 0;
    switch($item){
        case 'umbrella':
            $cost = 100;
            break;
        case 'jacket':
            $cost = 200;
            break;
        case 'mug':
            $cost = 50;
            break;
    }
    $tixD = $this->point_model->getTicks($_SESSION['playerID']);
    $tix = $tixD[0]->pticks;
    if($tix < $cost){
        $this->load->model('prize_model');
        $this->prize_model->setPrize($item,$_SESSION['playerID']);
        $newTix = $tix - $cost;
        $yes = $this->point_model->updateTicks($newTix,$_SESSION['playerID']);
        if($yes){
            $message = 'Ready to redeem your '.$item;
            $code = 900;
        }
    }else{
        $message = 'Insufficient tickets.';
        $code = 911;
    }
    $echo = array('message'=>$message,'code'=>$code);
    header('Content-Type: application/json',true);
    echo json_encode($echo);
}

编辑:将数组上的$ yes更改为$ message

我认为jquery:

$('.jacket-btn').click(function(){
        $.ajax({
            url: '<?php echo base_url('store/jacket'); ?>',
            method: 'POST',
            dataType: 'json',
            success: function(res){
                    console.log(res);
                    modal = $('#instructions .modal-body');
                    var child = '<span class = "note">'+res.message+'</span>';
                    modal.removeClass('instructions-content').addClass('notif');
                    $('.notif').append(child);
                    $('#instructions').modal('show');
            }
        });
    });

正如你所看到的,我使用了dataType:json和json_encode,但是如果变量来自else块,那么我只得到json对象,其中变量之前没有进程。为什么只有变量之前没有进程才有效?是因为我的模特回归吗?

2 个答案:

答案 0 :(得分:0)

也许是因为你的控制器功能需要使用json输出的CI方式:

public function redeem($item){
$this->load->model('point_model');
$cost = 0;
switch($item){
    case 'umbrella':
        $cost = 100;
        break;
    case 'jacket':
        $cost = 200;
        break;
    case 'mug':
        $cost = 50;
        break;
}
$tixD = $this->point_model->getTicks($_SESSION['playerID']);
$tix = $tixD[0]->pticks;
if($tix < $cost){
    $this->load->model('prize_model');
    $this->prize_model->setPrize($item,$_SESSION['playerID']);
    $newTix = $tix - $cost;
    $yes = $this->point_model->updateTicks($newTix,$_SESSION['playerID']);
    if($yes){
        $message = 'Ready to redeem your '.$item;
        $code = 900;
    }
}else{
    $message = 'Insufficient tickets.';
    $code = 911;
}
$echo['message'] = $message;
$echo['code'] = $code;

$this->output
->set_content_type('application/json')
->set_output(json_encode($echo));   
}

不确定,是。

答案 1 :(得分:0)

好的。

'{"message":"Ready to redeem your mug","code":900}'是正确的JSON字符串,但你需要原生的js对象,而不是json,是吗?

我认为你的问题在这里: 无论如何,$ .ajax都不处理响应 如果您需要使用本机JS对象:

try{
  var result = JSON.parse(res);
} catch(e) {console.error(e); }
console.log(result);

我不知道你的res中有什么可以崩溃console.log(res),认为在其他地方解析错误