Javascript 检查ajax调用返回的每一行
我有以下代码来对数据库执行查询。它返回一个对象列表,查询的每行结果一个:Javascript 检查ajax调用返回的每一行,javascript,json,ajax,Javascript,Json,Ajax,我有以下代码来对数据库执行查询。它返回一个对象列表,查询的每行结果一个: function getcontent() { var data = { "id": "<?php echo $stournid; ?>" }; data = $(this).serialize() + "&" + $.param(data); $.ajax({ type: "POST", dataType: "json"
function getcontent()
{
var data = {
"id": "<?php echo $stournid; ?>"
};
data = $(this).serialize() + "&" + $.param(data);
$.ajax({
type: "POST",
dataType: "json",
url: "response.php",
data: data,
success: function(response) {
//**************************** HERE!!!!
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
}
});
return false;
}
我想做的是,在Javascript中,逐个检查所有返回的行,并相应地更新一些div。我对如何迭代返回的行有困难
谢谢你的帮助。
谢谢
注意:就像一个循环,但更好。检查文档
不想使用forEach?
如果您不想使用forEach
,您可以使用一个旧的for
,如下所示:
success: function(response) {
// using for is not very pretty, hein?
for(var i = 0; i < response.length; i++) {
// response[i] is the i-th row of the array
var id = response[i].id;
var location = response[i].location;
var date = response[i].date;
// ... you get the idea
// do something with the current row (maybe create a div or table row ...)
});
},
成功:功能(响应){
//海因,用它不太好看吧?
对于(变量i=0;i
注意:就像一个循环,但更好。检查文档
不想使用forEach?
如果您不想使用forEach
,您可以使用一个旧的for
,如下所示:
success: function(response) {
// using for is not very pretty, hein?
for(var i = 0; i < response.length; i++) {
// response[i] is the i-th row of the array
var id = response[i].id;
var location = response[i].location;
var date = response[i].date;
// ... you get the idea
// do something with the current row (maybe create a div or table row ...)
});
},
成功:功能(响应){
//海因,用它不太好看吧?
对于(变量i=0;i
数据是什么样子的?发布一个JSON文件的示例!完成,用数据更新数据看起来如何?发布一个JSON文件的示例!完成,用数据更新
success: function(response) {
// redponse is an array of objects (so lets loop through it using forEach)
response.forEach(function(row) {
// row is a row (object) from the array
var id = row.id;
var location = row.location;
var date = row.date;
// ... you get the idea
// do something with the current row (maybe create a div or table row ...)
});
},
success: function(response) {
// using for is not very pretty, hein?
for(var i = 0; i < response.length; i++) {
// response[i] is the i-th row of the array
var id = response[i].id;
var location = response[i].location;
var date = response[i].date;
// ... you get the idea
// do something with the current row (maybe create a div or table row ...)
});
},