从XMLHttpRequest更改responseText

时间:2011-04-19 14:48:45

标签: javascript ajax

我使用此代码来跟踪来自我网站的所有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;

但它只适用于火,而不是铬。

任何人都可以帮我解决这个问题,或者知道另一种形式的跟踪任何ajax请求的responseText?

1 个答案:

答案 0 :(得分:1)

在IE上,您应该使用

obj.attachEvent(); 

代替

obj.addEventListener();