我正在尝试通过 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);
}
});