【问题标题】:jQuery: catch ajax exceptionsjQuery:捕获 ajax 异常
【发布时间】:2012-05-31 04:45:55
【问题描述】:

许多失败的 jQuery ajax 请求正在污染我的控制台并出现错误。查看产生这些控制台错误的代码(jQuery 1.7.2,第 8240 行)

                // Do send the request
                // This may raise an exception which is actually
                // handled in jQuery.ajax (so no try/catch here)
                xhr.send( ( s.hasContent && s.data ) || null );

我注意到评论解释了为什么那里没有try/catch。然而,即使我的jQuery.ajax 请求中有一个明确的error 回调函数,我仍然没有让jQuery.ajax 处理这些错误。

如何处理 jQuery ajax 错误,使错误消息显示在控制台中?

编辑:下面是我执行 ajax 请求的代码 sn-p,以及准确的错误消息(在 Chrome 中):

$.ajax({
    dataType: 'xml',
    url: "./python/perfdata.xml?sid=" + (new Date()),
    success: function (data) {
        var protocols = $("Protocols", data).children();

        parseData(protocols);
    },
    error: function (error) {
        setTimeout(getData, 200);
    }
});

这是 Chrome 错误消息:

GET
http://zeus/dashboard/python/perfdata.xml?sid=Thu%20May%2024%202012%2016:09:38%20GMT+0100%20(GMT%20Daylight%20Time)
jquery.js:8240

【问题讨论】:

  • 您能否同时发布创建 AJAX 请求的代码和完整的错误消息。
  • 您是否尝试在$.ajax 调用周围添加try/catch 块?我不确定这个错误是什么意思,但看起来它是在尝试发出请求时发生的,我认为 .ajaxError 仅适用于响应中的错误。
  • 会不会是这些错误是XML解析错误?
  • 我认为这些控制台消息根本不是例外,这些只是网络请求失败的通知。当你有 <img> 标签引用丢失的图像时,你会得到类似的结果。我怀疑你能压制他们。

标签: jquery ajax


【解决方案1】:

您可以使用自定义函数来执行此操作

 $(document).ajaxError(ajaxErrorHandler); 

并在该处理程序中设置您需要做的任何事情

var ajaxErrorHandler = function () {
        //do something
    }

【讨论】:

  • 好的,我试过了,但是ajaxErrorHandler 被执行控制台错误之上!
  • 谢谢!这真的帮助了我:)
【解决方案2】:

你试过了吗?

  $.ajaxSetup({
    timeout:10000,
    beforeSend: function(xhr) {
      //
    },
    complete:function(xhr,status) {
      //
    },      
    error: function(xhr, status, err) {
      switch (status){
      case 'timeout': {}
      case 'parseerror': {}
      case 'abort': {}
      case 'error': {}
      default: {}
      }
    }
  });

【讨论】:

    【解决方案3】:

    如果为了测试,你可以试试这个(在 chrome 中效果很好),通过覆盖函数“send”:

    $(function() {
    
        var xhr = null;
    
        if (window.XMLHttpRequest) {
            xhr = window.XMLHttpRequest;
        }
        else if(window.ActiveXObject('Microsoft.XMLHTTP')){
            // I do not know if this works
            xhr = window.ActiveXObject('Microsoft.XMLHTTP');
        }
    
        var send = xhr.prototype.send;
        xhr.prototype.send = function(data) {
            try{
                //TODO: comment the next line
                console.log('pre send', data);
                send.call(this, data);
                //TODO: comment the next line
                console.log('pos send');
            }
            catch(e) {
                //TODO: comment the next line
                console.log('err send', e);
            }
        };
    
        $.ajax({
            dataType: 'xml',
            url: "./python/perfdata.xml?sid=" + (new Date()).getTime(),
            success: function (data) {
                var protocols = $("Protocols", data).children();
    
                parseData(protocols);
            },
            error: function (error) {
                setTimeout(getData, 200);
            }
        });
    });
    

    test

    【讨论】:

      【解决方案4】:

      我收到 500 个内部服务器错误,当它指向 xhr.send( ( s.hasContent && s.data ) || null ); 行时

      但是当我检查 apache 日志(内部服务器错误)时,其他代码中出现了其他错误。

      请也检查一次。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-03
        • 1970-01-01
        • 2010-09-20
        • 2021-04-02
        • 1970-01-01
        • 1970-01-01
        • 2023-04-10
        相关资源
        最近更新 更多