1

我是 JavaScript 新手。我在其他人编写的代码中遇到问题。他们创建了一个 security.js 文件,并试图覆盖 xmlHTTPRequest(不确定)。在此处附上代码。

(function () {
    function getXHRObj(baseObj){
        var Impl = function(){          
            this.onreadystatechange = null;
            return this;
        };
        //Impl.prototype.onreadystatechange = null;
            // set the prototype of the new constructor
        Impl.prototype = baseObj;


        //Impl.prototype.constructor = Impl; // does not work in IE

            // the object to be returned
        var retObj = new Impl();

        function isHTTPReq(url){
            if(url){
                var colIndx = url.indexOf('://');
                if((colIndx < 0) 
                    || (url.substr(0, colIndx).toLowerCase().indexOf('http') == 0))
                    return true;
            }
            return false;
        }

            // customize open to add token in URL
            // even POST request should do this since POST may not always have key=value&... data format
        retObj.open = function(method, URL, async, user, pwd){

            if(isHTTPReq(URL)){         
                var prefix = (URL.indexOf('?') < 0)? '?' : '&';
                URL += (prefix + window.csrfToken);
            }
            //alert('making ajax to - ' + URL);
            Impl.prototype.open(method, URL, async, user, pwd);
        };

            // customize send
        retObj.send = function(body){

                /* Register the handler just before "send" to allow reuse of same object */
            Impl.prototype.onreadystatechange = function(){
                //alert('Impl.prototype.readyState- '+ Impl.prototype.readyState);

                    // copy the properties to return object
                if(Impl.prototype.readyState)
                    retObj.readyState = Impl.prototype.readyState;


                if(Impl.prototype.readyState == 4){
                    if(Impl.prototype.status)
                        retObj.status = Impl.prototype.status;

                    if(Impl.prototype.statusText)
                        retObj.statusText = Impl.prototype.statusText;

                    if(Impl.prototype.responseText)
                        retObj.responseText = Impl.prototype.responseText;

                    if(Impl.prototype.responseXML)
                        retObj.responseXML = Impl.prototype.responseXML;
                    //alert('xml done');
                }
                    // publish event to return object handler
                if(retObj.onreadystatechange){
                    //alert('invoking handler - \n' + retObj.onreadystatechange);
                    retObj.onreadystatechange();
                }else{
                    //alert('no handler');
                }
            };


            Impl.prototype.send(body);          
        };

            // delegate other methods
            /* Redefinition is necessary because IE does not allow inheritance 
               from native objects */
        retObj.abort = function() {
            Impl.prototype.abort();
        };

        retObj.setRequestHeader = function(hdr, val){
            Impl.prototype.setRequestHeader(hdr, val);
        };

        retObj.getResponseHeader = function(hdr){
            return Impl.prototype.getResponseHeader(hdr);
        };

        retObj.getAllResponseHeaders = function(){
            return Impl.prototype.getAllResponseHeaders();
        };

        return retObj;
    }

        // redefine the XMLttpRequest to use custom definition
    if(window.XMLHttpRequest){
        var Base_XMLHttpRequest = window.XMLHttpRequest;
        window.XMLHttpRequest = function(){
            //alert('in new XHR');
            return getXHRObj(new Base_XMLHttpRequest());
        };
    }

        // redefine the ActiveXObject to use custom definition
    if(window.ActiveXObject) {
        var Base_ActiveXObject = window.ActiveXObject;
        window.ActiveXObject = function(objType){
            //alert('in new ActiveXObj for - ' + objType);      
            if((objType.toLowerCase() != "microsoft.xmlhttp") 
                &&(objType.toLowerCase().indexOf("msxml2.xmlhttp") < 0)){
                    // return the standard impl for non-ajax objects
                return new Base_ActiveXObject(objType);
            }else{`enter code here`
                //alert('returning new impl for ' +  objType);
                return getXHRObj(new Base_ActiveXObject(objType));
            }
        };
    }
})();

此代码在 IE7 和 8 中运行良好,但在所有其他浏览器中都会出错。IE9 错误 -

SCRIPT65535:调用对象 security.js 无效,第 71 行字符 4

错误指向 this.onreadystatechange = null;

var Impl = function(){          
                this.onreadystatechange = null;
                return this;
            };

感谢即时帮助。谢谢!

4

0 回答 0