【问题标题】:json encode returns non-json objectjson 编码返回非 json 对象
【发布时间】:2013-10-08 20:08:04
【问题描述】:

我使用 $.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 对象。为什么它只有在变量之前没有过程的情况下才有效?是因为我的模特回归吗?

【问题讨论】:

  • 在 PHP 中嵌套的 if 中,if($yes){ 是否被处理过?
  • 是的,它已被处理,但当我 console.log 响应时,响应会像这样 {"message":...,"code": 900}。它应该是一个对象而不是 responseText。
  • 你错了'message'=&gt;$yes。应该是'message'=&gt;$message
  • @vp_arth 它应该仍然响应一个 json 对象而不仅仅是文本。 $yes 值为真
  • hm,好的...当 $tix =$cost, $yes 未定义...并且您没有在任何地方使用您的 $message...

标签: php jquery ajax json codeigniter


【解决方案1】:

好的。

'{"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) 崩溃,想想其他地方的解析错误

【讨论】:

  • 一开始遇到了解析错误。我应该把这个放在哪里?我已经很困惑了
  • 把所有这些放在你的result: function(res){ "here" }从结果回调中删除/注释所有其他代码。检查控制台。
  • 这段代码没有任何语法错误:$.ajax({ url: '/', method: 'POST', success: function(res){ console.log(res.substr(0, 100)); } });。尝试在浏览器控制台中的此页面上正确检查它
【解决方案2】:

可能是因为你的controller函数需要使用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));   
}

不确定,虽然。

【讨论】:

  • 如果这在客户端没有以这种方式为您输出任何内容,那么您的流程肯定有问题,您需要对其进行调试。
  • 不一样吗? header(...) 和 ->set_content_type() 一样吗?
  • 应该是,但你永远不知道何时使用框架。我在我的 CI 控制器上经常使用 json 并且它以这种方式完美地工作,所以它只是一个检查来确定。
  • 试过了。尽管如此,返回是这样的:{“message”:“准备兑换你的杯子”,“代码”:900},它仍然不是一个json,因为ajax没有运行成功函数。
  • 那么它在我的控制器上?
猜你喜欢
  • 2012-04-04
  • 1970-01-01
  • 2013-09-23
  • 2021-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-25
相关资源
最近更新 更多