无法覆盖XMLHttpRequest上的getResponseHeader方法 [英] Failed to override the getResponseHeader method on XMLHttpRequest

查看:154
本文介绍了无法覆盖XMLHttpRequest上的getResponseHeader方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图覆盖XMLHttpRequest对象上的 getResponseBody 方法。代码看起来如此:

I was trying to override the getResponseBody method on XMLHttpRequest object. The code looks so:

xhr.onreadyStateChange = function(){
    if (xhr.readyState !== 4) {
        return;
    }
    if (xhr.status === 200) {
        // callback to handle the result
    } else {
        var _orig = xhr.getResponseHeader;
        xhr.getResponseHeader = function(name){
            return decodeHeader(_orig.apply(xhr,[name]));
        };
        // callback to handle the failure
    }
}

调用 _orig.apply 时会抛出错误对象不支持此属性或方法。

It throws an error "Object doesn't support this property or method" when calling the _orig.apply.

任何的想法?谢谢。

PS:我每次创建一个新的XHR对象,不要重用旧的。

PS: I create a new XHR object every time and do not reuse the old one.

推荐答案

XMLHttpRequest 宿主对象(即环境提供的对象来表示环境的某些内容而不是语言的一部分),宿主对象基本上可以做任何他们喜欢的事情。它们不受与内置对象相同的规则的约束。 IE尤其值得注意的是利用这种自由,许多主机对象(包括 XMLHttpRequest )的行为并不像人们希望的那样。

XMLHttpRequest is a host object (i.e. an object provided by the environment to represent something about the environment rather than part of the language), and host objects can essentially do whatever they like. They're not bound by the same rules as built-in objects. IE is particularly notable for making use of this freedom and many of its host objects (including XMLHttpRequest) do not behave as one might hope.

因此,最好不要尝试添加或更改任何主机对象的任何属性。相反,编写自己的 XMLHttpRequest 包装器。

For this reason, it's better not to try and add or change any property of any host object. Instead, write your own XMLHttpRequest wrapper.

这篇关于无法覆盖XMLHttpRequest上的getResponseHeader方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆