Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无效的\u状态\u错误:DOM异常11_Javascript_Ajax_Xmlhttprequest - Fatal编程技术网

Javascript 无效的\u状态\u错误:DOM异常11

Javascript 无效的\u状态\u错误:DOM异常11,javascript,ajax,xmlhttprequest,Javascript,Ajax,Xmlhttprequest,我正在开发一个简单的辅助类来使用XmlHttpRequest发送请求(下面的代码)。但是我不能让它工作。例如,在google chrome上,我得到一个错误INVALID\u STATE\u ERR:DOM Exception 11,在其他浏览器上我得到一个状态==0 //@method XRequest: Object constructor. As this implements a singleton, the object can't be created calling the con

我正在开发一个简单的辅助类来使用XmlHttpRequest发送请求(下面的代码)。但是我不能让它工作。例如,在google chrome上,我得到一个错误
INVALID\u STATE\u ERR:DOM Exception 11
,在其他浏览器上我得到一个状态==0

//@method XRequest: Object constructor. As this implements a singleton, the object can't be created calling the constructor, GetInstance should be called instead
function XRequest() {
    this.XHR = XRequest.CreateXHR();
}
XRequest.instance = null;

//@method static GetInstance: Creates a singleton object of type XRequest. Should be called whenever an object of that type is required.
//@return: an instance of a XRequest object
XRequest.GetInstance = function() {
    if(XRequest.instance == null) {
        XRequest.instance = new XRequest();
    }
    return XRequest.instance;
}

//@method static CreateXHR: Implments a basic factory method for creating a XMLHttpRequest object
//@return: XMLHttp object or null
XRequest.CreateXHR = function() {
    var xhr = null;
    var factory = [
        function() { return new XMLHttpRequest(); },
        function() { return new ActiveXObject("Msxml2.XMLHTTP"); },
        function() { return new ActiveXObject("Microsoft.XMLHTTP"); }
    ];

    for(var i = 0; i < factory.length; ++i) {
        var f = factory[i];
        xhr = f();
        if(xhr) return xhr;
    }
    return null;
}

XRequest.prototype.SetRequestHeader = function(name, value) {
    if(this.XHR) {
        this.XHR.setRequestHeader(name, value);
    }
}

XRequest.prototype.SendRequest = function(args) {
    var async = true;
    var type = "";
    var url = "";
    var username = "";
    var password = "";
    var body = null;
    var success = null; 
    var failure = null;

    for(e in args) {
        switch(e) {
            case "async":
                async = args[e];
                break;
            case "type":
                type = args[e];
                break;
            case "success":
                success = args[e];
                break;
            case "failure":
                failure = args[e];
                break;
            case "url":
                url = args[e];
                break;
            case "username":
                username = args[e];
                break;
            case "password":
                password = args[e];
                break;
            case "body":
                body = args[e];
            break;
            case "setHeader":
                var h = args[e].split(":");
                if(h.length == 2) {
                    this.SetRequestHeader(h[0], h[1]);
                }
                break;
        }
    }

    var that = this;
    this.XHR.onreadystatechange = function() {
        alert("readyState == " + that.XHR.readyState + "  status == " + that.XHR.status);
        if(that.XHR.readyState == 4) {
            if(that.XHR.status == 200 || that.XHR.status == 0) {
                if(success) success(that.XHR);
            } else {
                if(failure) failure();
            }
        }
    };
    this.XHR.open(type, url, async, username, password);
    this.XHR.send(body);
}
/@method XRequest:对象构造函数。由于这实现了一个单例,因此不能通过调用构造函数来创建对象,应该调用GetInstance
函数XRequest(){
this.XHR=XRequest.CreateXHR();
}
XRequest.instance=null;
//@方法static GetInstance:创建XRequest类型的单例对象。应在需要该类型的对象时调用。
//@return:XRequest对象的实例
XRequest.GetInstance=函数(){
if(XRequest.instance==null){
XRequest.instance=新的XRequest();
}
返回XRequest.instance;
}
//@方法static CreateXHR:实现一个基本的工厂方法来创建XMLHttpRequest对象
//@返回:XMLHttp对象或null
XRequest.CreateXHR=函数(){
var xhr=null;
变量工厂=[
函数(){返回新的XMLHttpRequest();},
函数(){返回新的ActiveXObject(“Msxml2.XMLHTTP”);},
函数(){返回新的ActiveXObject(“Microsoft.XMLHTTP”);}
];
对于(变量i=0;i
用法示例:

<script language="javascript">
    function onLoad() {
        var x = XRequest.GetInstance();
        x.SendRequest({type:"GET",
            setHeader:"Accept:text/html, image/png, image/*, */*",
            url: "http://your_server.com/getData?param1=test",
            success:onSuccess, failure:onFail
        });
    }

    function onSuccess(obj) {
        alert("OK");                
    }

    function onFail() {
        alert("Not at this time!");
    }
</script>

函数onLoad(){
var x=XRequest.GetInstance();
x、 SendRequest({type:“GET”,
setHeader:“接受:text/html、image/png、image/*、*/*”,
url:“http://your_server.com/getData?param1=test",
成功:onSuccess,失败:onFail
});
}
成功时的功能(obj){
警报(“正常”);
}
函数onFail(){
警惕(“现在不行!”);
}

此ajax库中存在问题

XHR.setRequestHeader()
必须在
XHR.open()
之后调用

/@method XRequest:对象构造函数。由于这实现了一个单例,因此不能通过调用构造函数来创建对象,应该调用GetInstance
函数XRequest()
{
this.XHR=XRequest.CreateXHR();
}
XRequest.instance=null;
//@method static GetInstance:创建XRequest类型的singleton对象。应在需要该类型的对象时调用。
//@return:XRequest对象的实例
XRequest.GetInstance=函数()
{
if(XRequest.instance==null)
{
XRequest.instance=新的XRequest();
}
返回XRequest.instance;
}
//@method static CreateXHR:实现用于创建XMLHttpRequest对象的基本工厂方法
//@return:XMLHttp对象或null
XRequest.CreateXHR=函数()
{
var xhr=null;
变量工厂=[
函数(){返回新的XMLHttpRequest();},
函数(){返回新的ActiveXObject(“Msxml2.XMLHTTP”);},
函数(){返回新的ActiveXObject(“Microsoft.XMLHTTP”);}
];
对于(变量i=0;i// @method XRequest: Object constructor. As this implements a singleton, the object can't be created calling the constructor, GetInstance should be called instead
function XRequest()
{
    this.XHR = XRequest.CreateXHR();
}

XRequest.instance = null;


// @method static GetInstance: Creates a singleton object of type XRequest. Should be called whenever an object of that type is required.
// @return: an instance of a XRequest object
XRequest.GetInstance = function()
{
    if(XRequest.instance == null)
    {
        XRequest.instance = new XRequest();
    }

    return XRequest.instance;
}

// @method static CreateXHR: Implments a basic factory method for creating a XMLHttpRequest object
// @return: XMLHttp object or null
XRequest.CreateXHR = function()
{
    var xhr = null;
    var factory = [
                    function() { return new XMLHttpRequest(); },
                    function() { return new ActiveXObject("Msxml2.XMLHTTP"); },
                    function() { return new ActiveXObject("Microsoft.XMLHTTP"); }
                ];

    for(var i = 0; i < factory.length; ++i)
    {
        var f = factory[i];
        xhr = f();
        if(xhr)
            return xhr;
    }

    return null;
}

XRequest.prototype.SetRequestHeader = function(name, value)
{
    if(this.XHR)
    {
        //alert(name+'|||'+value);
        this.XHR.setRequestHeader(name, value);
    }
}

XRequest.prototype.SendRequest = function(args)
{
    var async = true;
    var type = "";
    var url = "";
    var username = "";
    var password = "";
    var body = null;
    var success = null; 
    var failure = null;

    for(e in args)
    {
        switch(e)
        {
            case "async":
                async = args[e];
                break;

            case "type":
                type = args[e];
                break;

            case "success":
                success = args[e];
                break;
            case "failure":
                failure = args[e];
                break;

            case "url":
                url = args[e];
                break;

            case "username":
                username = args[e];
                break;

            case "password":
                password = args[e];
                break;

            case "body":
                body = args[e];
                break;
        }
    }

    var that = this;
    this.XHR.onreadystatechange = function()
        {
            alert("readyState == " + that.XHR.readyState + "  status == " + that.XHR.status);
            if(that.XHR.readyState == 4)
            {
                if(that.XHR.status == 200 || that.XHR.status == 0)
                {
                    if(success)
                        success(that.XHR);
                }
                else
                {
                    if(failure)
                        failure();
                }
            }
        };

    this.XHR.open(type, url, async, username, password);
    for(e in args)
    {
        switch(e)
        {
            case "setHeader":
                var h = args[e].split(":");             
                if(h.length == 2)
                {
                    this.SetRequestHeader(h[0], h[1]);
                }
                break;
        }
    }
    this.XHR.send(body);
}
XRequest.prototype.SendRequest = function(params) {
    var defaultParams = {
        async:    true,
        type:     "",
        url:      "",
        username: "",
        password: "",
        body:     null,
        success:  null,
        failure:  null
    };

    for ( var i in defaultParams ) {
        if ( defaultParams.hasOwnProperty(i) && typeof params[i] == "undefined" ) {
            params[i] = defaultParams[i];
        }
    }

    var that = this;
    this.XHR.onreadystatechange = function() {
        if ( that.XHR.readyState == 4 ) {
            if ( that.XHR.status == 200 || that.XHR.status == 0 ) {
                if ( params.success ) {
                    params.success(that.XHR);
                }
            } else {
                if ( params.failure ) {
                    params.failure();
                }
            }
        }
    };

    this.XHR.open(
        params.type, parms.url, params.async, params.username, params.password
    );

    // It doesn't make sense to have a for/switch here when you're only handling
    // one case
    if ( params.setHeader ) {
        var h = params.setHeader.split(":");
        if ( h.length == 2) {
            this.SetRequestHeader(h[0], h[1]);
        }
    }

    this.XHR.send(params.body);
};
for ( var i in obj ) {
    if ( obj.hasOwnProperty(i) ) {
        // do stuff here
    }
}