处理JQuery ajax成功对象数据

时间:2017-01-10 18:25:19

标签: javascript jquery ajax coldfusion

我很难处理ajax对象传回来。我试图遍历每个对象并从每个对象输出每个数据值。

AJAX电话:

$.ajax({
  type: "POST",
  url: "sample.url",
  data: JSON.stringify(SDdata),
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(data) {

  console.log(data)

    $('#SD_Title').html(data.PagedData[1].SD_Plan_Name);
    $.each(data, function() {
      $.each(data, function(index) {
        console.log(data.PagedData[index].SD_Plan_Name);
        $('#SD_Content').html(data.PagedData[index]);
      });
    });

  },
  failure: function(errMsg) {
      alert(errMsg);
  }

});

到console.logs继续抛出undefined ??

我接收的数据

Array[3]
  0:Object
    SD_Plan_CreatedDate : "11/01/2016"
    SD_Plan_ID : 15
    SD_Plan_Name : "Jeff Harris D1 Replacement"
    SD_Plan_Status : 3
    SD_Plan_TotalCost : 75219.56
    SD_Plan_UnitCount : 268
  1:Object
  2:Object

编辑1:

Console.log(data) output

  Object
    PagedData:Array[3]
      0:Object
        SD_Plan_CreatedDate:"11/01/2016"
        SD_Plan_ID:15
        SD_Plan_Name:"Jeff Harris D1 Replacement"
        SD_Plan_Status:3 
        SD_Plan_TotalCost:75219.56
        SD_Plan_UnitCount:268
        __proto__:Object
      1:Object
      2:Object
      length:3
      __proto__:Array[0]
  Total:3
  __proto__:Object

3 个答案:

答案 0 :(得分:1)

我不明白你为什么需要嵌套的$.each()循环。你的数据结构太深了。我也不明白为什么你使用索引而不是给回调函数第二个值参数。请尝试以下方法:

$.ajax({
  type: "POST",
  url: "sample.url",
  data: JSON.stringify(SDdata),
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(data) {

  $('#SD_Title').html(data.PagedData[1].SD_Plan_Name);
    console.log(data.PagedData); // make sure you've got good data
    $.each(data.PagedData, function(index, value) {
      console.log(value); // look at the value of a single item in the array
      console.log(value.SD_Plan_Name); // If the data structure is correct this should be the value you're looking for
      $('#SD_Content').html(value);
    });
},
failure: function(errMsg) {
  alert(errMsg);
}

答案 1 :(得分:0)

正如你所说的数据是:

  Object
    PagedData:Array[3]
      0:Object
        SD_Plan_CreatedDate:"11/01/2016"
        SD_Plan_ID:15
        SD_Plan_Name:"Jeff Harris D1 Replacement"
        SD_Plan_Status:3 
        SD_Plan_TotalCost:75219.56
        SD_Plan_UnitCount:268
        __proto__:Object
      1:Object
      2:Object
      length:3
      __proto__:Array[0]
  Total:3
  __proto__:Object

所以试试

$.ajax({
  type: "POST",
  url: "sample.url",
  data: JSON.stringify(SDdata),
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(data) {

    $.each(data.PagedData, function(index, value) {

      $.each(value, function(index1 , value1) {
        console.log(value1);// each value wll be printed like "11/01/2016" then 15 and so on
     });

   });

  },
  failure: function(errMsg) {
      alert(errMsg);
  }
});

答案 2 :(得分:0)

我使用简单的for循环纠正了这个问题:

for( var i = 0; i < data.PagedData.length; i++){
  console.log(data.PagedData[i].SD_Plan_Name);
}