【问题标题】:Handling JQuery ajax success object data处理 JQuery ajax 成功对象数据
【发布时间】:2017-01-10 18:25:19
【问题描述】:

我很难处理 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

【问题讨论】:

  • 也许可以学习一些基本的调试技能,这样就不必每次不确定数据结构时都跑到堆栈溢出
  • console.log(data)
  • 不开玩笑,我知道如何调试 lol
  • 我认为你应该去 console.log(data.SD_Plan_Name);而不是 console.log(data.PagedData[index].SD_Plan_Name);
  • @codenut 抛出未定义的我试图循环遍历数组,然后遍历对象并输出每个对象的 SD_Plan_Name

标签: javascript jquery ajax coldfusion


【解决方案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);
}

【讨论】:

  • 这是回退 undefined: Object {Total: 3, PagedData: Array[3]} 3 undefined [Object, Object, Object] undefined
  • 好的,看起来您已经适当地编辑了原始帖子。我将对我的答案进行一些修改。
【解决方案2】:

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

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

【讨论】:

  • 试试$.each(data.PagedData, ...) 示例@dave 的答案。完美运行。
【解决方案3】:

正如你所说,你的数据是:

  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);
  }
});

【讨论】:

  • 返回未定义
  • 能否用成功的结果更新您的问题:function(data) { console.log(data); } 这将有助于发现问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-05
  • 2011-09-28
  • 2012-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多