18

多くの失敗した 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そこにないのかを説明していることに気づきました。ただし、リクエストに明示的なerrorコールバック関数がありますが、これらのエラーはまだ処理されていません。jQuery.ajaxjQuery.ajax

エラーメッセージがコンソールに表示されないように jQuery ajax エラーを処理するにはどうすればよいですか?

編集:以下は、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);
    }
});

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
4

4 に答える 4

6

カスタム関数を使用してこれを行うことができます

 $(document).ajaxError(ajaxErrorHandler); 

そのハンドラで必要なことは何でも設定します

var ajaxErrorHandler = function () {
        //do something
    }
于 2012-05-24T15:10:33.470 に答える
3

試しましたか?

  $.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: {}
      }
    }
  });
于 2012-05-30T14:07:28.897 に答える
3

関数「送信」をオーバーライドすることにより、テストの場合はこれを試すことができます(クロムでうまく機能します):

$(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);
        }
    });
});

テスト

于 2012-06-02T21:12:40.480 に答える