将xmlHttp响应类型设置为msxml-document会在IE11中引发无效状态错误

时间:2014-08-08 20:10:32

标签: javascript

当我尝试将XMLHTTPRequest对象响应类型设置为'ms-xml'时,我收到无效的状态错误。有谁知道为什么? 我附加了一些示例代码,用于创建对象和设置responseType属性。 感谢。

function getXHR() {
    if (window.XMLHttpRequest) {
        return (new window.XMLHttpRequest());
    } else {
        return (new ActiveXObject("Microsoft.XMLHTTP"));
    }
}

function test() {
    this._xmlhttp = getXHR();
        try {
            this._xmlhttp.open("POST", this.uri, this.async);
            this._xmlhttp.setRequestHeader("Content-Type", "text/xml");
            if (this.trace > 0) {
                SoapDbg.out("SOAP URI", this.uri);
            }
        } catch (ex) {
            throw (ex);
        }

        // form SOAPMethod header

        try {
            var method_header = this._action;
            if (this.trace > 0) {
                SoapDbg.out("SOAPMethodName", method_header);
            }
            this._xmlhttp.setRequestHeader("SOAPMethodName", method_header);
        } catch (ex) {
            throw (ex);
        }

        // form SOAPAction header

        try {
            var action_header = this._action;
            if (this.trace > 0) {
                SoapDbg.out("SOAPAction", action_header);
            }
            this._xmlhttp.setRequestHeader("SOAPAction", action_header);
        } catch (ex) {
            throw (ex);
        }

        if (this.trace > 0) {
            SoapDbg.out("SOAP Envelope", this._envelope);
        }

        // make the request

        try {
            if (this.async) {
                this._xmlhttp.onreadystatechange = this._onreadystatechange;
            }
            this._xmlhttp.send(this._envelope);
            try {
                this._xmlhttp.responseType = "msxml-document";
            } catch (e) {
                console.log("an error occured");
            }
        }
}

1 个答案:

答案 0 :(得分:0)

您可以尝试在open()之后设置responseType并重试...

this._xmlhttp.open("POST", this.uri, this.async);
try {
    this._xmlhttp.responseType = 'msxml-document';
} catch (e) {}
...

在设置responseType时,它取决于XmlHttpRequest的状态。