在JSON响应中变得未定义

时间:2019-01-04 07:51:07

标签: php json ajax codeigniter-3

我正在使用CodeIgniter购物车。我正在获取所有添加到购物车的信息,并在alert(data)中得到输出,但是我无法检查new ListView.builder( itemCount: 20, itemBuilder: (BuildContext context, int blockIdx) { print("Building block $blockIdx"); return new Column( children: [ Padding( child: Text("Block $blockIdx"), padding: EdgeInsets.all(8.0) ), ListView.builder( shrinkWrap: true, physics: ClampingScrollPhysics(), itemCount: 30, itemBuilder: (BuildContext context, int childIdx) { print("Building block $blockIdx child $childIdx"); return Padding( child: Text("Child $childIdx"), padding: EdgeInsets.only(left: 20.0, right: 8.0, top: 8.0, bottom: 8.0), ); }, ); ], ); }, ); 。我变得不确定。

o.qty

控制器

$(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');
              }
            }
          });
      });

我在警报(数据)中得到了输出

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();
}

但是当我访问o.qty时,我会得到{"outputStore":[{"id":"1","qty":1,"subtotal":5000,"removebtn":"c4ca4238a0b923820dcc509a6f75849b","cart_total":6000},{"id":"2","qty":1,"subtotal":1000,"removebtn":"c81e728d9d4c2f636f067f89cc14862c","cart_total":6000}]}

1 个答案:

答案 0 :(得分:0)

您应该从数据对象中获取数组。

$(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');
              }
            }
          });
      });