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

setting xmlHttp response type to msxml-document throws Invalid State Error in IE11

本文关键字:无效 错误 状态 msxml-document IE11 设置 xmlHttp 类型 响应      更新时间:2023-09-26

当我尝试将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");
            }
        }
}

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

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

这取决于您在设置responseType时XmlHttpRequest的状态