好吧,我有这个 ajax 代码,它将在 Success block 中返回来自 MySql 的结果。
$.ajax({
type:"POST",
url:"index.php",
success: function(data){
alert(data);
}
});
我的查询
$sql = "SELECT * FROM tablename";
$rs=parent::_executeQuery($sql);
$rs=parent::getAll($rs);
print_r($rs);
return $rs
我的响应数组在 AJAX 成功警报中
Array
(
[0] => Array
(
[section_id] => 5
[version] => 1
[section_name] => Crop Details
[id] => 5
[document_name] => Site Survey
[document_master_id] => 1
[document_section_id] => 5
)
[1] => Array
(
[section_id] => 28
[version] => 1
[section_name] => Vegetative Report
[id] => 6
[document_name] => Site Survey
[document_master_id] => 1
[document_section_id] => 28
)
)
我只想从结果中获取 section_name 和 document_name,以便我可以将这两个值附加到我的列表中。
最佳答案
不要使用print_r()
返回响应,使用json_encode()
:
echo json_encode($rs);
然后在 Javascript 中,您可以:
$.ajax({
type:"POST",
url:"index.php",
dataType: 'json'
success: function(data){
for (var i = 0; i < data.length; i++) {
console.log(data[i].section_name, data[i].document_name);
}
}
});
关于javascript - 在 jQuery AJAX 成功中从 MySql 获取特定响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27478808/