【问题标题】:jQuery: Handling Internal Server Error ResponsesjQuery:处理内部服务器错误响应
【发布时间】:2011-01-05 14:09:01
【问题描述】:

我在页面上有一个表单,它通过 jQuery 将新数据添加到数据库中。它工作正常,但有时服务器出现问题,我得到 500(内部服务器错误),但那是因为我的服务器很糟糕。

问题是我有时会在 PHP 添加到数据库后收到服务器错误,但即使添加已完成,我也没有收到成功消息,而是收到错误消息。那么如果我收到错误,我应该如何确保 PHP 不会添加数据呢?

[是的,我最终会切换到新服务器,但没有服务器是完美的]

这是脚本:

$.ajax({
  type: "POST",
  url: "/clase/do-add",
  data: $("#add").serialize(),
  dataType: "json",
  timeout: 8000,
  beforeSend: function() {
    var icon = '<p class="loading-add"></p>'; // loading icon
    $('form#add').append(icon);
  },
  success: function(data) {
    if (data.error == true) { // php returns error = true if name is invalid
      alert('Invalid name.');
      $('form#add').find('p').remove();
    } else {
      // make the addition
    }
  },
  error: function () { 
    $('form#add').find('p').remove();
    alert('Error. Try again.');
  }
});

【问题讨论】:

  • 是因为你的服务器很烂还是因为你的服务器端代码很烂?
  • 是你的服务器吗?你有 shell 访问权限吗?
  • 这是服务器,我在加载页面时不断收到各种字符 --> ‹ÄWÝrÔ6¾Þ} Å´´²7»äÏk'`¡lh

标签: php jquery internal-server-error


【解决方案1】:

您可以将 ajax 调用包装在一个函数中,并在出错时再次“请求”该函数。

某事链接这个:

jQuery.fn = function yourajaxfunction(){
    $.ajax({
      type: "POST",
      url: "/clase/do-add",
      data: $("#add").serialize(),
      dataType: "json",
      timeout: 8000,
      beforeSend: function() {
        var icon = '<p class="loading-add"></p>'; // loading icon
        $('form#add').append(icon);
      },
      success: function(data) {
        if (data.error == true) { // php returns error = true if name is invalid
          alert('Invalid name.');
          $('form#add').find('p').remove();
        } else {
          // make the addition
        }
      },
      error: function () { 
        $('form#add').find('p').remove();
        // ####### i added this:
        $(this).yourajaxfunction();
        alert('Error. Try again.');
      }
    });
}

或者您可以有一个空闲函数,在出现错误时再次运行该函数之前,该函数会在几毫秒后再次检查:

$(this).idle().yourajaxfunction();

上面用到的空闲函数:

jQuery.fn.idle = function(time){
    var o = $(this);
    o.queue(function(){
       setTimeout(function(){
          o.dequeue();
       }, time);
    });
    return o;
};

但最好的办法是修复服务器.. 这些东西只是弥补另一个问题的黑客和补丁:坏服务器。 :)

因此,这对于任何生产场景都不是一个好主意。

【讨论】:

  • 也许你需要检查最后插入 = 使用 jquery+ajax 来检查你的表单内容是否被保存/插入。但无论如何。这是一堆黑客。我会修理服务器。 :)
  • 服务器修复是最好的方法。我的 htaccess 中的 zlib 压缩导致内部服务器错误和奇怪的字符。谢谢!
【解决方案2】:

如果添加返回错误,您能否创建一个控件插入到数据库中。

   ...
   error: function () { 
        var fError = function() {

             $('form#add').find('p').remove();
             alert('Error. Try again.');

            };

       $.ajax({
          type: "GET",
          url: "/clase/do-check",
          data: {id: id}, // id for check
          dataType: "json",
          success: function(data) {
            if (!data.ok)
               fError();
          },
          error: function () { 
               fError();
          }
        });              
   }
   ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-13
    • 2013-12-05
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    相关资源
    最近更新 更多