AJAX: responseXML is null in IE 11
AJAX: responseXML is null in IE 11
使用以下脚本的AJAX调用在IE 11上失败。它适用于早期版本的IE-6(这是旧代码),7,8和9怀疑。它适用于Firefox 32.0.1,可能最早可以追溯到2(这是非常旧的代码)。
脚本在objXML = req.responseXML.documentElement;
处出错,错误为"无法获取未定义或null引用的属性"documentElement"。
调试时,req.responseXML为null,而req.responceText具有预期的XML。在搜索此问题时,解决方案的中心是响应类型不是"text/xml",但调试器显示响应类型是正确的"text/xxml"。
为什么req.responseXML为null?
var req;
function loadXMLDoc(url)
{
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send(null);
// branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send();
};
};
};
function processReqChange()
{
// only if req shows "complete"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
// ...processing statements go here...
//alert(req.responseText);
objXML = req.responseXML.documentElement;
// snip... using response data...
} else {
alert("There was a problem retrieving the XML data:'n'n" + req.statusText);
alert(req.status + "'n'n" + req.responseText);
};
};
};
原来Content-Type标头被设置了两次。返回XML的经典ASP页面包含以下内容以设置内容类型标题、带和大括号样式:
Call Response.AddHeader("Content-Type", "text/xml")
Response.ContentType = "text/xml"
IE的F12开发工具中网络视图的摘要选项卡将"类型"显示为"text/xml"。详细信息选项卡在"响应标头"中显示两个"内容类型"条目。IE 11不喜欢这样。删除第一个设置内容类型的调用并只保留Response.ContentType = "text/xml"
就解决了这个问题。
相关文章:
- window.opener not working in chrome & IE
- angularjs formly submit bug in safari & IE 11
- XMLHttpRequest in IE-7
- execScript try/catch in IE 8 中的怪异之处
- FireEvent in IE 11/Firefox
- InvalidStateError with msSaveBlob for json file in IE
- XMLHttpRequest in IE 8-9 with header
- .hasOwnProperty('getComputedStyle') false in IE 11
- Ajax call in Internet Explorer IE 7 & IE8 error
- AJAX: responseXML is null in IE 11
- Prevent fouc in firefox and ie
- jQuery ATTR funk in IE
- javascript in IE递归->window.frames不等于它自己
- 美元(文档).ready don't loading in IE
- File Object in IE 10
- JavaScript inside 'a' href in IE
- “this"in javascript在ie浏览器中的行为不同
- Moment.js YYYY-M-DD Invalid IN IE
- window.open() in IE 9
- ngFor not updating in IE 11 (RC4 with CoreJS)