从MySQL获得在jQuery的AJAX成功Specfic响应jQuery、MySQL、Specfic、AJAX

2023-09-11 22:32:18 作者:新不如旧

嗯,我有这样的阿贾克斯code这会从MySQL的成功块返回结果。

Well i have this ajax code which will return the result from MySql in Success block.

$.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的提醒我的反应阵列

My Response Array in alert of Success 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 的结果,这样我可以追加这两个值进入我的列表。

I want to get only section_name and document_name from the result so that i can append these two values to my list.

推荐答案

如果使用不返回响应的print_r(),使用 json_en code()

Don't return the response using print_r(), use json_encode():

echo json_encode($rs);

然后在Javascript中,你可以这样做:

Then in the Javascript, you can do:

$.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);          
        }
    }
});