标签:javascript ie10 ajax xml
使用Ajax请求,获取xml数据结构,在IE10、11会出现错误:
请求xml文件为:
在IE 10以上浏览器中:
IE9及以下浏览器返回:
查看文档,我们发现:
针对这种情况,我们需要对ajax请求进行重写,针对IE跟非IE使用不同的方式解析xml文档数据:
demo 1:
function $ajax(url, content, callback) { var xmlVer = ["MSXML2.XMLHTTP", "Microsoft.XMLHTTP"], xmlObj; if (cQuery.browser.isAllIE) { for (var i = 0; i < xmlVer.length; i++) { try { xmlObj = new ActiveXObject(xmlVer[i]); break; } catch (e) { } } } else { xmlObj = new XMLHttpRequest; } if (!xmlObj) { return; } xmlObj.open(content ? "POST" : "GET", url || location.href, !!callback); xmlObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); function getReturnValue() { return xmlObj.status == 200 ? /xml/i.test(xmlObj.getResponseHeader("content-type")) ? xmlObj.responseXML : xmlObj.responseText : null; } if (callback) { xmlObj.onreadystatechange = function () { if (xmlObj.readyState == 4) { var txt = getReturnValue(); if (callback(txt) === true) { setTimeout(function () { $ajax(url, content, callback); }, 1000); } } }; } xmlObj.send(content || ""); return callback ? xmlObj : getReturnValue(); }
demo 2:
var www = function (text) { try { xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async = "false"; xmlDoc.loadXML(text); } catch (e) { try //Firefox, Mozilla, Opera, etc. { parser = new DOMParser(); xmlDoc = parser.parseFromString(text, "text/xml"); } catch (e) { alert(e.message); return; } } alert(xmlDoc.text); //console.log(xmlDoc); //console.log(xmlDoc.text); } var xmlHttpReq = new XMLHttpRequest(); var realUrl = "http://172.16.86.171/rescorpmobile.3.4/html/wxj.xslt"; if (xmlHttpReq != null) { xmlHttpReq.open("GET", realUrl, true); xmlHttpReq.onreadystatechange = function () { if (xmlHttpReq.readyState == 4) { if (xmlHttpReq.status == 200) { //console.log(xmlHttpReq.responseText); www(xmlHttpReq.responseText); //console.log(loadXML(xmlHttpReq.responseText)); } } }; xmlHttpReq.send(null); }
Best
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:javascript ie10 ajax xml
原文地址:http://blog.csdn.net/xingxing1828/article/details/48028771