我正在尝试加载不在 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 为空...
有什么线索吗?