Laravel 4在控制器中发布数组json,来自jquery json

时间:2014-07-08 21:15:03

标签: php ajax arrays json laravel

尝试了多种方法获取从jquery发出的另一个JSON,其中生成的格式为:[{"id":"679","canti":"1","preci":"343.00"},{"id":"113","canti":"1","preci":"7.00"}]

但是在控制器中我检索空数组[] []?

<script type="text/javascript">
   $(document).ready(function(){
   $( "#sendForm" ).submit(function( event ) {
   event.preventDefault();

   var arrpedidos = [];
   ///// This data obtain from inputs texts an push in arrpedidos 
   $('#datatable tr.tfila').each(function () {
     var ttcod = $(this).find("td.tcodigo").html();
     var ttcan = $(this).find("td.tcantidad").html();
     var ttpre = $(this).find("td.tprecio").html();

     arrpedidos.push({
       id: ttcod, 
       canti:  ttcan,
       preci: ttpre
     });

    });
  ////here convert string and store in input field to send
  var myString = JSON.stringify(arrpedidos);

  $('#arreglofin').val(myString);
  ///// sending
  $.ajax({
     url: 'ajax/savePedido',
     type: 'POST',
     cache: false,
     dataType: 'json',
     data: myString,
     success: function(response){
        if (response.ok == 1) {
           alert('El pedido ha sido registrado con exito.');
        }else{
           alert('ERROR. No se ha sido registrado su pedido. Intente luego...');
             }; 
        }
    });

  });
 });
 </script>

控制器是:

 public function savePedido(){
    /// HOW LOOP AND GET ARRAY JSON???? I TRIED MANY WAYS 
    ///$input = Input::except('_token');
    ///$input = (object)Input::all();
    ///$json = json_decode($input);

    try {
        foreach ($input as $key => $value) {
           Log::info('LOLO.'. $value); //// no write in log, because i cannot get JSON CORRECTLY
        }
         return json_encode(array('ok' => '1'));
    }
    catch(\Exception $e){
         return json_encode(array('ok' => '0')); 
    }
 }

谢谢,我希望你的帮助

0 个答案:

没有答案