javascript - INVALID_STATE_ERR : DOM Exception 11

标签 javascript ajax xmlhttprequest

我正在开发一个简单的辅助类来使用 XmlHttpRequest 发送请求(代码如下)。但我无法让它发挥作用。例如,在谷歌浏览器中,我收到错误 INVALID_STATE_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);
}

使用示例:

<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>

最佳答案

此 ajax 库中的问题。

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

// @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);
}

关于javascript - INVALID_STATE_ERR : DOM Exception 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2357430/

相关文章:

javascript - 删除 Internet Explorer 中的滚动条

javascript - 如何在 Chart.js 中制作预填充 donut ?

php - 如何将字符串保存在mysql数据库中

Javascript JSON解析对象,一个对象属性未定义,不抛出任何错误

javascript - 当 Asnyc 设置为 true 时,XMLHttpRequest 不起作用

javascript - 不可见对象的高度,当它们变得可见时,在 javascript 中

javascript - 如何在 onClick 事件期间更改 React 中多个元素的状态

javascript - 使用ajax将数据从TinyMCE保存到php

Jquery - 如何发出 ifModified :true work in . ajax 请求?

javascript - 使用 D3 的 XHR/发布请求