web-dev-qa-db-ja.com

INVALID_STATE_ERR:DOM例外11

XmlHttpRequestを使用してリクエストを送信する簡単な補助クラスを開発しています(以下のコード)。しかし、私はそれを機能させることができません。たとえば、Google Chromeではエラー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>
16
Andres

いずれにせよ、巨大なSendRequestを使用する代わりに、ミックスインを作成することでswitchメソッドを簡略化できます。

_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..in_ループには、2つの明確な問題があります。

  1. varを使用しておらず、グローバルが作成されている:for (e in args)for (var e in args)である必要があります
  2. _for..in_を使用する場合は常に、各キーがオブジェクトの直接のメンバーであり、プロトタイプによって誤って継承されたものではないことを常に確認する必要があります。

_for ( var i in obj ) {
    if ( obj.hasOwnProperty(i) ) {
        // do stuff here
    }
}
_
9
Justin Johnson

この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);
}
40
ukostin

通常、このエラーは、async = trueでopenメソッドを呼び出すか、asyncパラメーターを未定義のままにしてデフォルトで非同期にしてから、statusまたはresponseTextプロパティにアクセスすると、XMLHttpRequestで発生します。これらのプロパティは、同期呼び出しを行った後、または準備状態(非同期呼び出しが応答した後)になったときにのみ使用できます。最初にasync = falseで試し、次にtrueに切り替えてonReadyStateChangeを使用することをお勧めします。

6
Dave Lampert

私の場合、xhr.statusTextメソッド内でxhr.onreadystatechangeにアクセスしようとしたときにエラーが発生しましたが、xhr.readyStateの取得はうまくいきました。

3
Temperage