【问题标题】:change responseText from a XMLHttpRequest从 XMLHttpRequest 更改 responseText
【发布时间】:2011-04-19 14:48:44
【问题描述】:

我使用此代码来跟踪来自我的站点的所有 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 请求的 responseText 吗?

【问题讨论】:

  • 你能把你从 chrome 或 IE 的调试控制台看到的东西贴出来吗?
  • 我不知道答案,但这很酷......

标签: javascript ajax


【解决方案1】:

在 IE 上,你应该使用

obj.attachEvent(); 

改为

obj.addEventListener();

【讨论】:

  • Christian in ie with firebug lite 显示此 [44] 被拦截打开(POST,afhostmedia.com/info.php,true,未定义,未定义)并停止。在 FF [894] 拦截打开 (POST , afhostmedia.com/info.php , true , undefined , undefined) [894] 捕获发送 (valor=Hello+H) POST afhostmedia.com/info.php 200 OK 289ms 日志: [894] 捕获: 200 NOM: esto : 你好H
  • dyoser,您不应该尝试编辑答案;将您的 cmets 输入为 cmets(就像您所做的那样),这是正确的方法。
猜你喜欢
  • 1970-01-01
  • 2013-06-02
  • 2011-03-26
  • 2014-02-04
  • 1970-01-01
  • 2011-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多