dongyang9813 2019-01-04 07:51
浏览 66

在JSON响应中获取未定义

I am using the CodeIgniter shopping cart. I am fetching the all the add to cart information and I am getting the output in the alert(data) but I am not able to check the o.qty. I am getting undefined.

$(document).ready(function() {
      $.ajax({
          url: "<?php echo base_url(); ?>Member_controller/primaryCartload",
          context: document.body,
          success: function(data) {
            //alert(data);

            if (data != 0) {
              console.log(data);
              alert(data);
              var obj = JSON.parse(data);
              $.each(obj, function(i, o) {
                  alert(o.qty);
                  if (o.qty != 0) {
                    $('#subtotal_details').html('Total cost:' + o.subtotal);
                    //alert('not empty');
                  });
              }
              else {
                //alert('empty')
                $('#totalDetails').html('0');
                $('#totalQty').html('Total items:0');
              }
            }
          });
      });

Controller

public function primaryCartload()
 {
  $output=[];
  $count = 0;

  foreach($this->cart->contents() as $items)
  {
   $count++;
   $output[] = array(
            'id' =>$items["id"],
            'qty' =>$items["qty"],
            'subtotal'=>$items["subtotal"],
            'removebtn'=>$items["rowid"],
            'cart_total'=>$this->cart->total()
            );
  }
  $outputStore['outputStore']=$output;
  if($count == 0)
  {
   $outputStore ['outputStore']= 0;
  }
 echo json_encode($outputStore);
 exit();
}

I am getting the output in the alert(data)

{"outputStore":[{"id":"1","qty":1,"subtotal":5000,"removebtn":"c4ca4238a0b923820dcc509a6f75849b","cart_total":6000},{"id":"2","qty":1,"subtotal":1000,"removebtn":"c81e728d9d4c2f636f067f89cc14862c","cart_total":6000}]}

but when I am accessing the o.qty then I am getting undefined

  • 写回答

1条回答 默认 最新

  • duanren9163 2019-01-04 08:00
    关注

    You should take the array from data object.

    $(document).ready(function() {
          $.ajax({
              url: "<?php echo base_url(); ?>Member_controller/primaryCartload",
              context: document.body,
              success: function(data) {
                //alert(data);
    
                if (data != 0) {
                  console.log(data);
                  alert(data);
                  var obj = JSON.parse(data);
                  $.each(obj["outputStore"], function(i, o) {
                      alert(o.qty);
                      if (o.qty != 0) {
                        $('#subtotal_details').html('Total cost:' + o.subtotal);
                        //alert('not empty');
                      });
                  }
                  else {
                    //alert('empty')
                    $('#totalDetails').html('0');
                    $('#totalQty').html('Total items:0');
                  }
                }
              });
          });
    
    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法