如何在jQuery ajax中访问多个对象数据

时间:2018-10-04 14:38:17

标签: javascript jquery

我正在将数据从后端以数组的形式传递给jQuery ajax。现在我实际上同时以数组形式传递了两个对象,现在我正尝试访问每个数据并显示在表中。数据由3个不同的对象组成,包括Form,Field_type和meta。两个值meta和form在同一个对象中,而form数据在另一个对象中,我如何在表上显示它呢?这是我的代码在底部,请先帮助谢谢。 错误:

  dynamicFieldForm:642 Uncaught TypeError: Cannot read property 'length' of 
  undefined
   at Object.success (dynamicFieldForm:642)
   at u (app.js:1)
   at Object.fireWith [as resolveWith] (app.js:1)
   at r (app.js:1)
   at XMLHttpRequest.<anonymous> (app.js:1)
   success @ dynamicFieldForm:642  
   u @ app.js:1 
   fireWith @ app.js:1
   r @ app.js:1
   (anonymous) @ app.js:1
   load (async)
   send @ app.js:1
   ajax @ app.js:1
   ajax @ dynamicFieldForm:605

我的代码:

function ajax(){
    var label = $('input[class^="label_field"]').map(function() {
    return this.value;
}).get();

var options = $('input[class^="options"]').map(function() {
    return this.value;
}).get();

var select = $('[class^="selected"]').map(function() {
    return this.value;
}).get();

$.ajax({
    type: "POST",
    headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    url: "http://localhost:8000/dynamicForm/fields",
    data: {// change data to this object
    _token : $('meta[name="csrf-token"]').attr('content'), 
    label: label,
    select:select,
    options:options
  },

    success: function(resultData){
      $("#row").empty();
      var id = 0;
      for(var i=0; i<resultData.length; i++){                  
          if(resultData[i].length>1){           
             for(var j=-1; j<resultData[i].length; j++){
                 if(resultData[j].length>1){  
                       //this is the code that generate error..
                      id++;
                      for(var k=0; k<resultData[j].length-1; k++){
                          $('#row').append("<tr id=tr"+id+"></tr><tr><td>"+ resultData[i][j][k]['label'] +"</td> <td>"+ resultData[i][j][k]['options'] +"</td>  <td id='field'>"+resultData[i][j][k]['field_type_id']+"</td></tr>");                                       
                      }
                 }
                    //and it is not working       
          console.log(resultData[i][j]['name']);
          }
        }
      }
    },
  });
  //after click submit button fields of form will remove...
  $('form').empty();
  //this for to show the button again otherwise button will not show...
  bool = true;
}

0 个答案:

没有答案