[英]Get Specfic response from MySql in jQuery AJAX Success
Well i have this ajax code which will return the result from MySql in Success block. 好吧,我有这个ajax代码,它将从Success块中的MySql返回结果。
$.ajax({
type:"POST",
url:"index.php",
success: function(data){
alert(data);
}
});
My Query 我的查询
$sql = "SELECT * FROM tablename";
$rs=parent::_executeQuery($sql);
$rs=parent::getAll($rs);
print_r($rs);
return $rs
My Response Array in alert of Success AJAX 我的响应数组在成功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
)
)
I want to get only section_name and document_name from the result so that i can append these two values to my list. 我想从结果中只获取section_name和document_name ,以便我可以将这两个值附加到我的列表中。
Don't return the response using print_r()
, use json_encode()
: 不要使用print_r()
返回响应,请使用json_encode()
:
echo json_encode($rs);
Then in the Javascript, you can do: 然后在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);
}
}
});
使用更改您的选择查询
$sql = "SELECT section_name,document_name FROM tablename";
$sql = "SELECT * FROM tablename";
$rs=parent::_executeQuery($sql);
$rs=parent::getAll($rs);
return json_encode($rs);
index.php 的index.php
$.ajax({
type:"POST",
url:"index.php",
success: function(data){
alert(data);
var data = JSON.parse(data);
/* you can use $.each() function here */
}
});
Do this: 做这个:
$sql = "SELECT * FROM tablename";
$rs=parent::_executeQuery($sql);
$rs=parent::getAll($rs);
$resp = array();
foreach( $rs as $each ){
$resp[]['section_name'] = $each['section_name'];
$resp[]['document_name'] = $each['document_name'];
}
return json_encode($resp);
Access JSON response like this: 像这样访问JSON响应:
for (var i = 0; i < data.length; i++) {
console.log(data[i].section_name, data[i].document_name);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.