Javascript 处理jqueryajax成功对象数据
我很难处理ajax对象的返回。我试图在每个对象上循环并输出每个对象的每个数据值 AJAX调用:Javascript 处理jqueryajax成功对象数据,javascript,jquery,ajax,coldfusion,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)
$.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.log继续抛出未定义的
我正在接收的数据
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
我不明白为什么需要嵌套的$。每个循环。您对数据结构的了解太深了。我也不明白为什么要使用索引而不是给回调函数第二个value参数。请尝试以下操作:
$.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);
}
正如您所说,您的数据是:
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);
}
});
我使用一个简单的for循环纠正了这个问题:
for( var i = 0; i < data.PagedData.length; i++){
console.log(data.PagedData[i].SD_Plan_Name);
}
也许你可以学习一些基本的调试技巧,这样你就不必每次对数据控制台的结构不确定的时候都跑来堆去了;而不是console.logdata.PagedData[index].SD\u Plan\u Name@codenut抛出undefined im尝试循环数组,然后循环对象,并输出每个对象的SD_Plan_名称这是在抛出undefined:Object{Total:3,PagedData:array[3]}3 undefined[Object,Object,Object]undefinedOk,loos,就像您已经适当地编辑了原始帖子一样。我将对我的答案进行一些编辑。返回未定义请使用成功的结果更新您的问题:functiondata{console.logdata;}它将有助于查找问题尝试$.eachdata.PagedData。。。示例@dave的答案。工作完美。