首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Ajax时处理MySQL错误

使用Ajax时处理MySQL错误
EN

Stack Overflow用户
提问于 2013-08-13 14:33:23
回答 2查看 5K关注 0票数 1

在PHP代码中添加了头和Ajax中的捕获,但仍然没有joy.

在使用Ajax处理表单提交时,我很难处理MySQL错误。我有以下代码:

代码语言:javascript
复制
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');
                        }
          }

    }); 

这样可以很好地处理任何错误,如缺页等。

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

目前我已经尝试过这样的方法:

代码语言:javascript
复制
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脚本中,以便将其显示给用户(对吗?)--我不知道如何做到这一点。

有什么建议吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-13 16:39:07

我会将您所有的错误逻辑从jQuery移到PHP中。如果希望提供特定的结果,您可以使用一个简单的JSON对象进行响应,该对象可以保存status (成功或错误)、code (如果需要)、message,甚至data

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

代码语言:javascript
复制
$.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类似的内容:

代码语言:javascript
复制
$result = array(
  'response' => array(
    'status' => 'error',
    'code' => '1', // whatever you want
    'message' => 'Could not connect to the database.'
  )
);    

echo json_encode($result);

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

希望这能有所帮助!

票数 3
EN

Stack Overflow用户

发布于 2013-08-13 14:40:24

发送503头

代码语言:javascript
复制
<?php
header('HTTP/1.1 503 Service Unavailable');

你的ajax代码应该能用。

编辑:-它将使用您的失败代码,您可以签入该函数的状态代码503。

代码语言:javascript
复制
jqXHR.status

将是503和

代码语言:javascript
复制
jqXHR.responseText

会包含你的信息。

Edit2: js.file

代码语言:javascript
复制
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:

代码语言:javascript
复制
<?php
header('HTTP/1.1 503 Service Unavailable');
echo('hallo welt');
exit();

结果:

代码语言:javascript
复制
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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18211954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档