在 PHP 代码和 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 脚本中以将其显示给用户(对吗?) - 我不确定如何执行此操作。
任何指针/建议?