gpt4 book ai didi

jquery - 使用 Ajax 时处理 MySQL 错误

转载 作者:行者123 更新时间:2023-11-29 04:09:05 24 4
gpt4 key购买 nike

向 PHP 代码添加了 header 并在 Ajax 中进行了捕获,但仍然没有任何乐趣。

在使用 Ajax 处理表单提交时,我无法集中精力处理 MySQL 错误。我有以下代码:

var url = "save.php"; // the script where you handle the form input.

$.ajax({
type: "POST",
url: url,
data: $("#frmSurvey").serialize(), // serializes the form's elements.


success: function(jqXHR, textStatus, errorThrown){

$('.sequence-container div').hide().delay( 2000 );
$next.show().delay( 2000 );



},
error: function(jqXHR, textStatus, errorThrown){
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 503) {
alert('Error: ' + jqHXR.responseText);
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404] - Click \'OK\' and try to re-submit your responses - if this error box re-appears, call 01255 850051 and we will assist.');
} else if (jqXHR.status == 500) {
alert('Internal Server Error. [500] - Click \'OK\' and try to re-submit your responses - if this error box re-appears, call 01255 850051 and we will assist');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed - Click \'OK\' and try to re-submit your responses - if this error box re-appears, call 01255 850051 and we will assist');
} else if (exception === 'timeout') {
alert('Time out error - Click \'OK\' and try to re-submit your responses - if this error box re-appears, call 01255 850051 and we will assist');
} else if (exception === 'abort') {
alert('Ajax request aborted - Click \'OK\' and try to re-submit your responses - if this error box re-appears, call 01255 850051 and we will assist');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText + ' - Click \'OK\' and try to re-submit your responses - if this error box re-appears, call 01255 850051 and we will assist');
}
}

});

这工作正常并处理任何错误,例如页面丢失等。

但是,我完全不知道如何处理/向用户显示实际提交到数据库时可能出现的任何错误。

目前我试过这个:

if ($mysql_error!="") {
header('HTTP/1.1 503 Service Unavailable');
printf("Unexpected database error: %s\n", $mysql_error);
mysqli_stmt_close($proc);
mysqli_clean_connection($link);
exit();
}

但由于页面未显示(因为它是由 Ajax 提交的),所以不会出现错误消息。我想我必须将该错误消息带回到 Ajax 脚本中以将其显示给用户(是吗?)- 我不确定该怎么做。

有任何指示/建议吗?

最佳答案

我会将您所有的错误逻辑从 jQuery 移至 PHP。您可以使用一个简单的 JSON 对象进行响应,该对象可以包含 status(成功或错误)、code(如果需要)、message,甚至data,如果你想提供具体的结果。

例如,您发出这样的请求:

$.ajax({
type: 'POST',
url: url,
data: $("#frmSurvey").serialize(),
success: function(result){
var json = $.parseJSON(result);
if(json.response.status == 'success') {
// do something
} else {
// look at message or code to perform specific actions
}
}
});

然后在处理此请求的 PHP 文件中,您构建一个包含上述所有所需元素(状态、代码、消息等)的数组。最终,您将 echo 像这样:

$result = array(
'response' => array(
'status' => 'error',
'code' => '1', // whatever you want
'message' => 'Could not connect to the database.'
)
);

echo json_encode($result);

$result 数组将包含基于您在 PHP 中进行的检查的相关数据。

希望这对您有所帮助!

关于jquery - 使用 Ajax 时处理 MySQL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18211954/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com