获得jQuery AJAX中MySql的特定响应

时间:2022-10-09 11:47:23

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

我有这个ajax代码,它会在Success block中返回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,以便可以将这两个值附加到列表中。

4 个解决方案

#1


3  

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

#2


2  

change your select query with

使用以下命令更改select查询

$sql = "SELECT section_name,document_name FROM tablename";

#3


2  

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

#4


1  

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

#1


3  

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

#2


2  

change your select query with

使用以下命令更改select查询

$sql = "SELECT section_name,document_name FROM tablename";

#3


2  

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

#4


1  

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