繁体   English   中英

laravel从JSON响应获取未定义的对象数据

[英]laravel get object data from JSON response giving undefined

我正在使用ajax通过控制器从模型获取数据,每件事都运行良好,我认为当我使用console.log(response)检查时,我得到了

Object {buyTemps: Object}

在对象旁边,我发现了数组中的所有数据到目前为止,每件事都很好

阿贾克斯

     $(".buy-tr").click(function(e){ 
    var 
      data = {}, 
      $row = $(this).closest("tr"),            // Finds the closest row <tr> 
      $tds = $row.find("td:nth-child(1)");     // Finds the 2nd <td> element
    $.each($tds, function() {                  // Visits every single <td> element
        data={buyId:$(this).text()};                     
    }); 
  //  $(this).find('input:radio').prop('checked', true); 

    $(".buy-tr").click(function(e){ 
    var 
      data = {}, 
      $row = $(this).closest("tr"),         // Finds the closest row <tr> 
      $tds = $row.find("td:nth-child(1)");  // Finds the 2nd <td> element
    $.each($tds, function() {               // Visits every single <td> element
        data={buyId:$(this).text()};                     
    });  
    $.ajax({
        url : "/buy/selectTable",
        type : 'GET',
        dataType : 'json',
        data : data,
    success : function(response) { 
            console.log(response);

       $('#buyItem-table tbody').empty();
        $.each(response,function(index, v){
           $('#buyItem-table tbody').append(                                          

                  "<tr><td>"         +  v.buyItemTempId   
                      + "</td><td>"  +  v.itemName    
                      + "</td><td>"  +  v.itemExpire    
                      + "</td><td>"  +  v.buyPrice                          
                      + "</td><td>"  +  v.buyBox   
                      + "</td><td>"  +  v.itemPacking                           
                      + "</td><td>"  +  v.buyQty       
                 + "</td></tr>" );          

           });


    },
    error : function(response) {
      swal("error"); 
    }

在我的控制器

     public function selectItemTable()
    {

        $buyId = Input::get('buyId'); 
        $buyTemps = DB::table('vbuytemp')->where('buyId',$buyId)->paginate(10);      
         return Response::json(compact('buyTemps'));    

}

consol.log(v)

  Object {total: 1, per_page: 10, current_page: 1, last_page: 1,       next_page_url: null…}current_page: 1data: Array[1]0: Objectbarcode: "08815408"buyBox: 30buyId: 2buyItemTempId: 2buyPrice: "2.500"buyQty: 90itemExpire: "2018-01-04"itemId: 2itemName: "Panadol Extra tab"itemPacking: 2minQty: 1roofId: 1sellingForm: 1__proto__: Objectlength: 1__proto__: Array[0]from: 1last_page: 1next_page_url: nullper_page: 10prev_page_url: nullto: 1total: 1__proto__: Object__defineGetter__: __defineGetter__()__defineSetter__: __defineSetter__()__lookupGetter__: __lookupGetter__()__lookupSetter__: __lookupSetter__()constructor: Object()hasOwnProperty: hasOwnProperty()isPrototypeOf: isPrototypeOf()propertyIsEnumerable: propertyIsEnumerable()toLocaleString: toLocaleString()toString: toString()valueOf: valueOf()get __proto__: get __proto__()set __proto__: set __proto__() 

结果所有表单元格都用未定义填充

您的问题是您在错误的位置遍历了响应数据。 当您调用$.each时,实际上是遍历所有分页元数据(总计,per_page等)。 所需的实际数据包含在数据数组中。 因此,只需遍历该数组即可,它应该可以工作。

$.each(response.data,function(index, v){
    //now v.buyItemTempId and other properties exist
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM