0

我正在尝试通过 AJAX 调用从我的数据库中返回多行数据。它成功返回一行,但有不止一行,即使它成功检索数据,我也会收到错误消息。我已将其范围缩小到服务器端问题,这很可能存在于我的 PHP 中。这是我正在使用的 PHP 函数:

    public function get_case_studies($conn) {

    $v_sql_str = <<< END_SQL
        SELECT 
            name,
            title,
            content,
            location
        FROM case_studies
        WHERE is_active_flag = 1 AND is_deleted_flag = 0  
    END_SQL;

    try {
        $sth = $conn->prepare($v_sql_str);
        $sth->execute();
        }
        catch (PDOException $e) {
            $v_errormsg=$e->getMessage();
            return <<< END_HD
            {   
                "status":"FAIL" ,
                "error": "db_exception: Could not select case studies from database",
                "errorno": "1003" ,
                "errortype":  "SYS",
                "usermsg": "SQL=$v_sql_str, Error = $v_errormsg"
            }
    END_HD;
    }

    $v_json = "\"data\" : [";
    $n=0;
    $v_QT="\"";         
    while ($result = $sth->fetch(PDO::FETCH_OBJ)) {

        if ($n > 0) { $v_json = $v_json . ",";};
        $v_flags = "";

        $v_json = $v_json . "{ "
        . $v_QT .   "name" .                    $v_QT . ":" .       $v_QT . $result->name .                                 $v_QT . "," 
        . $v_QT .   "title" .                   $v_QT . ":" .       $v_QT . $result->title .                                $v_QT . ","
        . $v_QT .   "content" .                 $v_QT . ":" .       $v_QT . base64_decode($result->content) .               $v_QT . ","
        . $v_QT .   "location" .                $v_QT . ":" .       $v_QT . $result->location .                             $v_QT 
        . "}" ;
        $n++;
    }
    $v_json = $v_json . "] ";

    $sth->closeCursor();

    $v_json1 = "{ \"status\" : \"OK\", " . $v_json . " }";
    return $v_json1;

}

这是我的 AJAX 调用:

            $.ajax({
            type: "POST",
            url: "ajax.php",
            data: { module: "case_studies.php", action: "get_case_studies" }, 
            success: function(json_data) {
                ui_case_studies.case_list = json_data.data;
                alert(JSON.stringify(json_data.data));
            },
            error: function (a, b, c) {
                console.log(JSON.stringify(a));
                alert("Something went wrong while retreiving the case studies, please contact your database administrator.");
                //alert(JSON.stringify(a));
                //alert(b);
                //alert(c);
            }
        });
4

1 回答 1

1

json_encode创建 JSON 时最好使用它。还要确保您是否正确解析了您的Ajax' onSuccess部分中返回的数据。

PHP部分:

$result = array(); 
$i = 0;

while ($result = $sth->fetch(PDO::FETCH_OBJ)) {
    $result[$i]['name'] = $result->name;
    $result[$i]['title'] = $result->title;
    $result[$i]['content'] = base64_decode($result->content);
    $result[$i]['location'] = $result->location;
    $i++;
}

return json_encode($result);

JS部分:

new Ajax.Request(url,
{
    method: 'post',
    onSuccess: function(transport) 
    {
        var response = transport.responseText || false;

        if (response !== false) {
            var result = JSON.parse(response);

            alert(result[1]['name']); // etc.
        }
    }
});

编辑:

您没有提供其他信息。有错误不是有用的答案。所以,这里有一些东西可以在调试时帮助你。

PHP 响应格式:

[
  {
    "name": "name0",
    "title": "title0",
    "content": "content0",
    "location": "location0"
  },
  {
    "name": "name1",
    "title": "title1",
    "content": "content1",
    "location": "location1"
  },
  {
    "name": "name2",
    "title": "title2",
    "content": "content2",
    "location": "location2"
  }
]

解析:

var response = '[{"name":"name0","title":"title0","content":"content0","location":"location0"},{"name":"name1","title":"title1","content":"content1","location":"location1"},{"name":"name2","title":"title2","content":"content2","location":"location2"}]';
var result = JSON.parse(response);

alert(result[1]['name']);
于 2013-02-27T09:29:09.320 回答