在Codeigniter中使用JSON将数据传递到服务器

时间:2014-12-05 20:25:04

标签: javascript php json codeigniter

我试图将存储在Javascript数组中的一些数据传递给服务器进行数据库处理。我使用的是codeignier。

Javascript数组(unitdata_set): -

[{"unit_id":"13","unit_title":"Testsdsdf","unit_max_occupancy":"3","unit_no":"1","unit_no_adults":"1","unit_no_children":"1","unit_no_extrabed":0,"unit_mealtype":"Full Board","unit_fullboard_adult":"6000.00","unit_fullboard_child":"4000.00","unit_halfboard_adult":"1800.00","unit_halfboard_child":"1200.00","room_total":"81600.00","all_room_price":"81600.00","extra_bed_price":"4.00","meal_price":22000,"saving_amount":16320,"discounted_bedroom_price":65280},{"unit_id":"14","unit_title":"Res2","unit_max_occupancy":"12","unit_no":"0","unit_no_adults":"0","unit_no_children":"0","unit_no_extrabed":0,"unit_mealtype":"Bed and Breakfast","unit_fullboard_adult":"9000.00","unit_fullboard_child":"7000.00","unit_halfboard_adult":"7000.00","unit_halfboard_child":"5000.00","room_total":"86400.00","all_room_price":"0.00","extra_bed_price":"","meal_price":0,"saving_amount":0,"discounted_bedroom_price":0}] 

Ajax功能: -

  function sendBooking(){

          var boooking_url = "<?php echo site_url('tempbooking/addBooking'); ?>";
          var data_booking = '';
          data_booking = JSON.stringify(unitdata_set);

          $.post(boooking_url,{

            'booking_data[]':data_booking

          },function(data){
            console.log(data);

          });

      }

我的控制器功能,用于接收json数据: -

function addBooking(){
            $booking_data=$this->input->post('booking_data');

             foreach ($booking_data as $booking) {

                             $booking_attr = json_decode($booking);
                        echo $booking_attr->unit_id . " " . $booking_attr->unit_title;
                        //echo $booking_attr['unit_id'] . " " . $booking_attr['unit_title'];
                }


        }

我收到的错误消息: -

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Trying to get property of non-object</p>
<p>Filename: controllers/tempbooking.php</p>
<p>Line Number: 156</p>

</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Trying to get property of non-object</p>
<p>Filename: controllers/tempbooking.php</p>
<p>Line Number: 156</p>

</div>  

Print_r: -

print_r($this->input->post('booking_data'));

Array
(
    [0] => [{"unit_id":"13","unit_title":"Testsdsdf","unit_max_occupancy":"3","unit_no":"1","unit_no_adults":"1","unit_no_children":"1","unit_no_extrabed":0,"unit_mealtype":"Full Board","unit_fullboard_adult":"6000.00","unit_fullboard_child":"4000.00","unit_halfboard_adult":"1800.00","unit_halfboard_child":"1200.00","room_total":"81600.00","all_room_price":"81600.00","extra_bed_price":"4.00","meal_price":22000,"saving_amount":16320,"discounted_bedroom_price":65280},{"unit_id":"14","unit_title":"Res2","unit_max_occupancy":"12","unit_no":"0","unit_no_adults":"0","unit_no_children":"0","unit_no_extrabed":0,"unit_mealtype":"Bed and Breakfast","unit_fullboard_adult":"9000.00","unit_fullboard_child":"7000.00","unit_halfboard_adult":"7000.00","unit_halfboard_child":"5000.00","room_total":"86400.00","all_room_price":"0.00","extra_bed_price":"","meal_price":0,"saving_amount":0,"discounted_bedroom_price":0}]
)

3 个答案:

答案 0 :(得分:0)

也许是因为您的JSON对象在数组中?

[ {JSON} ]

尝试修改您的php函数,以便访问数组中的JSON:

function addBooking(){
    $booking_data=$this->input->post('booking_data');

    foreach ($booking_data as $booking) {
        //NEW:
        $booking_attr = json_decode($booking[0]);

        echo $booking_attr->unit_id . " " . $booking_attr->unit_title;
    }
}

答案 1 :(得分:0)

尝试更改您的控制器

function addBooking()
{
    $booking_data=json_decode($this->input->post('booking_data'));
    foreach ($booking_data as $booking) 
    {   
        $booking_attr = ($booking);
        echo $booking_attr->unit_id . " " . $booking_attr->unit_title;
        //echo $booking_attr['unit_id'] . " " . $booking_attr['unit_title'];
    }
}

并改变你的javascript代码

function sendBooking()
{
      var boooking_url = "<?php echo site_url('tempbooking/addBooking'); ?>";
      var data_booking = '';
      data_booking = (unitdata_set);

      $.post(boooking_url,{

        'booking_data':data_booking

      },function(data){
        console.log(data);

      });

  }

答案 2 :(得分:0)

由于某种原因,json_decode不解码包含对象的数组。如果您使用javascript数组将其传递给服务器端/ php,请先将数组添加到对象中。按照以下步骤完成此操作。

  1. 请将您的数组添加到对象中。如下: -

    var unitdata_set = [];
    
    var obj_userbooking_data = {"bookings":unitdata_set}; //Added the above array into a new object and passed it to the server
    
  2. 确保JSON.stringify对象。

    data_booking = JSON.stringify(obj_userbooking_data);

  3. 因此功能变为

    function sendBooking(){
    
    
                        var boooking_url = "<?php echo site_url('booking/addBooking'); ?>";
                        var data_booking = '';
                        data_booking = JSON.stringify(obj_userbooking_data);
    
                        $.post(boooking_url,{
    
                          'booking_data':data_booking
    
                        },function(data){
                              console.log(data);
    
                        });
    
    
    
                  }
    
    1. 使用json_decode解码转换为JSON字符串的对象,在客户端使用JSON.stringify。因此,您的服务器端/ php / Codeigniter控制器功能变为: -

          function addBooking(){
               $booking_data=$this->input->post('booking_data');
               $booking_data=json_decode($booking_data,true); 
               echo 'Your Data: ' . $booking_data[0]['unit_id'];
       }