English 中文(简体)
jj 查询: 捕捉 ajax 例外
原标题:jQuery: catch ajax exceptions
  • 时间:2012-05-24 15:08:18
  •  标签:
  • jquery
  • ajax

许多jQuery ajax 请求失败的 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 / captain captain >。 但是,尽管在我的 jQuery.ajax 请求中,我有一个明确的 eror 回调功能, I m still 没有由 jQuery.ajax 处理这些错误。

我如何处理jQuery ajax 错误, 使错误信息在控制台中出现 non ?

Edit: 下面是我的代码片断,它满足了ajax的要求, 以及准确的错误信息(在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);
    }
});

以下是铬误差信息:

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

您可以尝试这个( 铬效果良好), 如果用于测试, 您可以在函数“ 发送” 上跳过 :

$(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

问题回答

您可以使用自定义函数来这样做

 $(document).ajaxError(ajaxErrorHandler); 

并在该处理器中设置任何您需要做的

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

你试过吗?

  $.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: {}
      }
    }
  });

我得到了500个内部服务器错误, 当它指向线 xhr. send (( s. hasContent& & amp; s. data) \\ ne; 时, 我得到了500个内部服务器错误;

但当我检查 apache 日志( 内部服务器错误) 时, 错误在其它代码中是其它的 。

请也检查一次。





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...