Php Ajax-数据库连接处理
在执行ajax请求时(对于post方法),我试图通过PHP连接到我的数据库,但如果用户无法连接,我想向用户显示一条错误消息。当它连接时,我可以显示成功消息,但我想知道是否有办法手动抛出catch块,这样我就知道我的代码正在工作,并且如果用户无法连接到数据库(目前正在使用console.log进行测试),我会向用户显示一条消息 JSPhp Ajax-数据库连接处理,php,ajax,Php,Ajax,在执行ajax请求时(对于post方法),我试图通过PHP连接到我的数据库,但如果用户无法连接,我想向用户显示一条错误消息。当它连接时,我可以显示成功消息,但我想知道是否有办法手动抛出catch块,这样我就知道我的代码正在工作,并且如果用户无法连接到数据库(目前正在使用console.log进行测试),我会向用户显示一条消息 JS $.ajax({ type: "post", url: "test.php", dataType: "j
$.ajax({
type: "post",
url: "test.php",
dataType: "json",
error: function(data) {
console.log(data.status);
console.log("Not Successful Test");
if (data.status == "connectionError") {
console.log("Didn't connect to database");
} else {
console.log("Other");
}
},
success: function(data) {
console.log(data.status);
console.log("Successful Test");
if (data.status == "success") {
console.log("Connected to Database");
} else {
console.log("Other");
}
}
});
$.ajax({
type: "post",
url: "test.php",
dataType: "json",
error: function(data) {
console.log(data.responseText);
},
success: function(data) {
console.log(data.status);
console.log("Successful Test");
if (data.status == "success") {
console.log("Connected to Database");
} else {
console.log("Other");
}
}
});
test.php
try {
require_once("dbConn.php");
$dbConn = getConnection();
$response_array["status"] = "success";
} catch (Exception $e) {
$response_array["status"] = "connectionError"; // Want to display this response in JS code
}
header("Content-type: application/json");
echo json_encode($response_array);
function getConnection()
{
try {
$conn = new PDO(
"localhost=localhost;dbname=dbname",
"username",
"password"
);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $conn;
} catch (Exception $e) {
throw new Exception("Connection error " . $e->getMessage(), 0, $e);
}
}
try {
require_once("dbConn.php");
$dbConn = getConnection();
$response_array["status"] = "success";
} catch (Exception $e) {
// Added http response code then die() with message
http_response_code(503);
die("<b>Server Error.</b><br/> This service is currently unavailable. Please try again at a later time.");
}
header("Content-type: application/json");
echo json_encode($response_array);
dbConn.php
try {
require_once("dbConn.php");
$dbConn = getConnection();
$response_array["status"] = "success";
} catch (Exception $e) {
$response_array["status"] = "connectionError"; // Want to display this response in JS code
}
header("Content-type: application/json");
echo json_encode($response_array);
function getConnection()
{
try {
$conn = new PDO(
"localhost=localhost;dbname=dbname",
"username",
"password"
);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $conn;
} catch (Exception $e) {
throw new Exception("Connection error " . $e->getMessage(), 0, $e);
}
}
try {
require_once("dbConn.php");
$dbConn = getConnection();
$response_array["status"] = "success";
} catch (Exception $e) {
// Added http response code then die() with message
http_response_code(503);
die("<b>Server Error.</b><br/> This service is currently unavailable. Please try again at a later time.");
}
header("Content-type: application/json");
echo json_encode($response_array);
解决方案
test.php
try {
require_once("dbConn.php");
$dbConn = getConnection();
$response_array["status"] = "success";
} catch (Exception $e) {
$response_array["status"] = "connectionError"; // Want to display this response in JS code
}
header("Content-type: application/json");
echo json_encode($response_array);
function getConnection()
{
try {
$conn = new PDO(
"localhost=localhost;dbname=dbname",
"username",
"password"
);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $conn;
} catch (Exception $e) {
throw new Exception("Connection error " . $e->getMessage(), 0, $e);
}
}
try {
require_once("dbConn.php");
$dbConn = getConnection();
$response_array["status"] = "success";
} catch (Exception $e) {
// Added http response code then die() with message
http_response_code(503);
die("<b>Server Error.</b><br/> This service is currently unavailable. Please try again at a later time.");
}
header("Content-type: application/json");
echo json_encode($response_array);
为了通知浏览器请求未成功,您需要传递一个非2xx的http响应代码。出于您的目的,您可以使用例如503
http_response_code(503);
这将确保浏览器理解调用的输出不是成功的(默认值为200)
在该语句之后,如果希望错误消息由PHP层驱动,您仍然可以返回一些JSON。查看浏览器的开发控制台,它将始终包含HTTP 200…如果您想在JS Ajax中使用“error”函数,您必须发送HTTP代码!=对不起你能详细说明一下吗?我不确定您所说的“成功”和“错误”的AJAX处理程序是由HTTP状态代码触发的。因此,无论您的PHP脚本是否可以建立DB连接,您都会发送一个有效的答案(HTTP状态为200)。这是否意味着当处理程序使用有效的状态代码响应用户无法连接到数据库时,无法获取错误消息,但正如我所说的:您必须发送4xx的HTTP状态代码来触发AJAX处理程序中的“error”方法。请参阅HTTP状态代码列表:我尝试将以下代码添加到test.php中的catch块中,但仍然收到200的响应代码,尽管我已从web服务器中删除了dbConn.php文件。您可能会看到,由于缺少该文件,php输出了一些调试信息。您需要使用浏览器的开发人员控制台来查看出了什么问题。在Chrome中,按ctrl+shift+i、ten-go networking选项卡,然后选择XHR部分。然后启动Ajax请求;您将看到它与响应代码一起显示在那里(如果您想查看PHP返回的确切内容,请在选择请求后选中Preview选项卡)。