1

在 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 脚本中以将其显示给用户(对吗?) - 我不确定如何执行此操作。

任何指针/建议?

4

2 回答 2

3

发送 503 标头

<?php
header('HTTP/1.1 503 Service Unavailable');

并且您的 ajax 代码应该可以工作。

编辑: 它将使用您的失败代码,您可以检查状态代码 503 的功能。

jqXHR.status

将是 503 和

jqXHR.responseText

将包含您的消息。

编辑2 : 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');
                }
  }

});

保存.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
于 2013-08-13T14:40:24.883 回答
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 中进行的检查的相关数据。

希望这可以帮助!

于 2013-08-13T16:39:07.190 回答