我有以下ajax,如下所示:
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
data: JSON.stringify(json),
contentType: 'application/json; charset=utf-8',
success: function (Data) {
alert(JSON.stringify(Data));
},
error: error
});
当我做警报时(JSON.stringify(Data)); 它显示了下面的数据。数据是我发回的对象。它有3列,有4列。我如何遍历stringify中的数据,因为我需要将其存储到HTML表中?
{"Data":[{"Iden":"12","Date":"01/23/2011","City":"Clearwater","State":"FL"},{"Iden":"19","Date":"02/09/2012","City":"Elgin","State":"IL"},{"Iden":"14","Date":"06/22/2010","City":"Newport Beach","State":"CA"}]}
答案 0 :(得分:2)
你想使用jQuery的每个函数:
$.each( Data, function(index, element){
alert( "Index: " + index + ", Element: " + element );
});
答案 1 :(得分:0)
在将它传递给$ .each函数之前,你必须解析JSON数据
var d = JSON.parse(Data);
You can also directly call like this too
$.each( d.Data, function(index, element){
alert( "Index: " + index + ", Element: " + element );
});
For ex if the json is like this
{"note":{"to":"Tove","from":"Jani","heading":"Reminder","body":"Don't forget me this weekend!"}}
$.each( d.note, function(index, element){
alert( "Index: " + index + ", Element: " + element );
});