Javascript 从XMLHttpRequest更改responseText

Javascript 从XMLHttpRequest更改responseText,javascript,ajax,Javascript,Ajax,我使用这段代码跟踪来自我的站点的所有ajax请求 XMLHttpRequest.prototype.uniqueID = function( ) { if (!this.uniqueIDMemo) { this.uniqueIDMemo = Math.floor(Math.random( ) * 1000); } return this.uniqueIDMemo; } XMLHttpRequest.prototype.oldOpen = XMLHttp

我使用这段代码跟踪来自我的站点的所有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;
但它只能在火上工作,不能用ie或chrome

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

在IE上,您应该使用

obj.attachEvent(); 
反而

obj.addEventListener();

你能在chrome或IE的调试控制台上发布你能看到什么吗?我不知道答案,但这很酷…Christian在IE和firebug lite中展示了[44]截获打开(post,,true,undefined,undefined)和停止。在FF[894]截获打开(POST,,true,undefined,undefined)[894]捕获发送(valor=Hello+H)POST 200 OK 289ms日志:[894]捕获:200 NOM:esto:Hello Hdyser,您不应该尝试编辑答案;以注释的形式输入您的注释(就像您那样),这是正确的方法。