在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脚本以显示给用户(是吗?) - 我不知道该怎么做。
任何指针/建议?
答案 0 :(得分:3)
发送503-Header
<?php
header('HTTP/1.1 503 Service Unavailable');
并且你的ajax代码应该可以工作。
修改强> 它将使用您的失败代码,您可以在函数中检查状态代码503.
jqXHR.status
将是503和
jqXHR.responseText
将包含您的消息。
<强> EDIT2:强> js.file
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){
if (jqXHR.status == 404) {
alert('Error: ' + jqHXR.responseText);
}else{
$('.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 == 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 (errorThrown === '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 (errorThrown === '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 (errorThrown === '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');
}
}
});
save.php:
<?php
header('HTTP/1.1 503 Service Unavailable');
echo('hallo welt');
exit();
结果:
Uncaught Error.
hallo welt - Click 'OK' and try to re-submit your responses - if this error box re-appears, call 01255 850051 and we will assist
答案 1 :(得分:3)
我会把你的所有错误逻辑从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中进行的检查的相关数据。
希望这有帮助!