几年前我遇到了同样的问题,然后我放弃了responseXML并开始始终使用responseText。这个解析函数一直对我有用:
function parseXml(xmlText){ try{ var text = xmlText; //text = replaceAll(text,"<","<"); //text = replaceAll(text,">",">"); //text = replaceAll(text,""","""); //alert(text); //var myWin = window.open('','win','resize=yes,scrollbars=yes'); //myWin.document.getElementsByTagName('body')[0].innerHTML = text; if (typeof DOMParser != "undefined") { // Mozilla, Firefox, and related browsers var parser=new DOMParser(); var doc=parser.parseFromString(text,"text/xml"); //alert(text); return doc; }else if (typeof ActiveXObject != "undefined") { // Internet Explorer. var doc = new ActiveXObject("Microsoft.XMLDOM"); // Create an empty document doc.loadXML(text); // Parse text into it return doc; // Return it }else{ // As a last resort, try loading the document from a data: URL // This is supposed to work in Safari. Thanks to Manos Batsis and // his Sarissa library (sarissa.sourceforge.net) for this technique. var url = "data:text/xml;charset=utf-8," + enpreURIComponent(text); var request = new XMLHttpRequest(); request.open("GET", url, false); request.send(null); return request.responseXML; } }catch(err){ alert("There was a problem parsing the xml:n" + err.message); }}
使用此XMLHttpRequest对象:
// The XMLHttpRequest class objectdebug = false;function Request (url,oFunction,type) { this.funct = ""; // this.req = ""; this.url = url; this.oFunction = oFunction; this.type = type; this.doXmlhttp = doXmlhttp; this.loadXMLDoc = loadXMLDoc;}function doXmlhttp() { //var funct = ""; if (this.type == 'text') { this.funct = this.oFunction + '(req.responseText)'; } else { this.funct = this.oFunction + '(req.responseXML)'; } this.loadXMLDoc(); return false;}function loadXMLDoc() { //alert(url); var functionA = this.funct; var req; req = false; function processReqChange() { // alert('reqChange is being called'); // only if req shows "loaded" if (req.readyState == 4) { // only if "OK" if (req.status == 200) { // ...processing statements go here... eval(functionA); if(debug){ var debugWin = window.open('','aWindow','width=600,height=600,scrollbars=yes'); debugWin.document.body.innerHTML = req.responseText; } } else { alert("There was a problem retrieving the data:n" + req.statusText + 'nstatus: ' + req.status); if(debug){ var debugWin = window.open('','aWindow','width=600,height=600,scrollbars=yes'); debugWin.document.body.innerHTML = req.responseText; } } } } // branch for native XMLHttpRequest object if(window.XMLHttpRequest) { try { req = new XMLHttpRequest(); } catch(e) { req = false; } // branch for IE/Windows ActiveX version } else if(window.ActiveXObject) { try { req = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { req = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { req = false; } } } if(req) { req.onreadystatechange = processReqChange; if(this.url.length > 2000){ var urlSpl = this.url.split('?'); req.open("POST",urlSpl[0],true); req.setRequestHeader('Content-Type','application/x-www-form-urlenpred'); req.send(urlSpl[1]); } else { req.open("GET", this.url, true); req.send(""); } }}function browserSniffer(){ if(navigator.userAgent.toLowerCase().indexOf("msie") != -1){ if(navigator.userAgent.toLowerCase().indexOf("6")){ return 8; }else{ return 1; } } if(navigator.userAgent.toLowerCase().indexOf("firefox") != -1){ return 2; } if(navigator.userAgent.toLowerCase().indexOf("opera") != -1){ return 3; } if(navigator.userAgent.toLowerCase().indexOf("safari") != -1){ return 4; } return 5;}
当然,这是很旧的代码,但是它在我几年前建立的网站上仍然对我有用。尽管现在我通常使用框架,但我与其他所有人都同意,因此我不再需要使用此代码或类似的代码。
您可以使用Request
onreadystate函数中的split等忽略某些细节。如果请求的长度大于某个长度,应该将其转换为帖子,但是我只是认为做一个帖子总是更好。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)