изменить responseText из XMLHttpRequest - PullRequest
0 голосов
/ 19 апреля 2011

Я использую этот код для отслеживания всех запросов AJAX с моего сайта

XMLHttpRequest.prototype.uniqueID = function( ) {

    if (!this.uniqueIDMemo) {
        this.uniqueIDMemo = Math.floor(Math.random( ) * 1000);
    }
    return this.uniqueIDMemo;
}

XMLHttpRequest.prototype.oldOpen = XMLHttpRequest.prototype.open;

var newOpen = function(method, url, async, user, password) {
    console.log("[" + this.uniqueID( ) + "] intercepted open (" +
            method + " , " +
            url + " , " +
            async + " , " +
            user + " , " +
            password + ")");
    this.oldOpen(method, url, async, user, password);
}

XMLHttpRequest.prototype.open    = newOpen;
XMLHttpRequest.prototype.oldSend = XMLHttpRequest.prototype.send;

var newSend = function(a) {
    var xhr = this;
    console.log("[" + xhr.uniqueID( ) + "] interceptando envio (" + a + ")");

    var onload = function( ) {
        console.log("[" + xhr.uniqueID( ) + "] interceptando respuesta: " +
            xhr.status +
            " " + xhr.responseText.replace(/Pues/g,"NOM:"));
        //xhr.responseText = "BitBox => "+xhr.responseText;

        /*var xhr = { // mock object
                aborted: 0,
                responseText: "esto y lo otro....",
                responseXML: null,
                status: 0,
                statusText: 'n/a',
                getAllResponseHeaders: function() {},
                getResponseHeader: function() {},
                setRequestHeader: function() {},
                abort: function() {
                    log('aborting upload...');
                    var e = 'aborted';
                    this.aborted = 1;
                    $io.attr('src', s.iframeSrc); // abort op in progress
                    xhr.error = e;
                    s.error && s.error.call(s.context, xhr, 'error', e);
                    g && $.event.trigger("ajaxError", [xhr, s, e]);
                    s.complete && s.complete.call(s.context, xhr, 'error');
                }
            };*/
    };

    var onerror = function( ) {
        console.log("[" + xhr.uniqueID( ) + "] interceptando error: "+xhr.status);
    };

    xhr.addEventListener("load", onload, false);
    xhr.addEventListener("error", onerror, false);

    xhr.oldSend(a);
}

XMLHttpRequest.prototype.send = newSend;

Но он работает только в огне, а не в Chrome.

Любой может помочь мне с этой проблемойили знает другую форму отслеживания responseText любого ajax-запроса?

1 Ответ

1 голос
/ 19 апреля 2011

В IE вы должны использовать

obj.attachEvent(); 

вместо

obj.addEventListener();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...