我正在尝试加载一个XML文档,该文档不在Web应用程序文件夹中,而是通过GET请求向上加载。 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文件中添加访问控制允许来源
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
但仍然收到警报消息 onError 与响应文本为空...
有什么线索吗?
感谢您的回复...实际上我的问题是其他...似乎IE无法处理XML文件中存在的UTF-16编码...更改为 UTF-8,现在一切都很好