我正在迭代我的JSON响应。我有HTML元素,id为参数名称。
例如,我的JSON响应包含"costcenter":"1234"
,而且<span>
元素的标识为costcenter
。
现在,我没有为每个id编写语句,而是认为我可以遍历JSON数组,并自动读取其名称。
这就是我得到的
$(".dataset").click(function() {
changeid = this.id;
$.ajax({
url: "source",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: {
id: changeid
},
success: function(data) {
// How to get the name of the parameter, and then read it's value?
}
})
})
JSON看起来像这样,它只是一个维度和一个结果集:
{"changeid":"1","costcenter":"478","manager":"John Smith"}
答案 0 :(得分:3)
如果我理解你的问题,你在成功函数中需要类似的东西:
for (var key in data) {
$('#'+key).html(data[key]);
}