XDomainRequest无法在IE9中运行

时间:2012-09-21 13:15:32

标签: get httpwebrequest xmlhttprequest cors xdomainrequest

我正在尝试加载不在Web应用程序文件夹中但通过GET请求升级的XML文档。 loadXMLDoc适用于FF和chrome,但XDR不适用于IE。

我正在调用这样的方法:

xmlDoc = loadXMLDoc("../XML/stops_group1.xml");

我认为问题在于在根目录中上升一级,因为它适用于相同的目录文件夹

function loadXMLDoc(url) {

    if (typeof XDomainRequest != 'undefined') {

        var xdr = new XDomainRequest();
        xdr.contentType = "text/plain";
        xdr.timeout = 5000;
        if (xdr) {
            xdr.onerror = function () {
                alert('XDR onerror');
                alert("Got: " + xdr.responseText);
            };
            xdr.ontimeout = function () {
                alert('XDR ontimeout');
                alert("Got: " + xdr.responseText);
            };
            xdr.onprogress = function () {
                alert("XDR onprogress");
                alert("Got: " + xdr.responseText);
            };
            xdr.onload = function () {
                alert('onload' + xdr.responseText);
                callback(xdr.responseText);
            };
            // 2. Open connection with server using GET method
            xdr.open("get", url);
            // 3. Send string data to server
            xdr.send("");
        } else {
            alert('failed to create xdr');
        }
        return xdr.responseXML;
    }
    var xhr = createCORSRequest('GET', url);
    if (!xhr) {
        throw new Error('CORS not supported');
    }

    xhr.send("");
    return xhr.responseXML;
}

function createCORSRequest(method, url) {
    var xhr = new XMLHttpRequest();
    if ("withCredentials" in xhr) {
        // Check if the XMLHttpRequest object has a "withCredentials" property.
        // "withCredentials" only exists on XMLHTTPRequest2 objects.
        xhr.open(method, url, true);
    } else if (typeof XDomainRequest != "undefined") {
        // Otherwise, check if XDomainRequest.
        // XDomainRequest only exists in IE, and is IE's way of making CORS requests.
        xhr = new XDomainRequest();
        xhr.open(method, url);
    } else {
        // Otherwise, CORS is not supported by the browser.
        xhr = null;
    }
    return xhr;
}

我已经尝试在web.config文件中添加Access-Control-Allow-Origin

<system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>

但仍然会收到onError的警告消息,其中responseText为空...

任何线索?

1 个答案:

答案 0 :(得分:0)

感谢回复......实际上我的问题是其他的...似乎IE无法处理xml文件中存在的UTF-16编码...改为utf-8而evertything现在很好