对本地XML文件IE JQuery的AJAX parseerror

编程入门 行业动态 更新时间:2024-10-25 20:22:33
本文介绍了对本地XML文件IE JQuery的AJAX parseerror的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在IE浏览器,jQuery是给我的时候,我试图读取本地XML文件中的parseError。希望有人也许能够发现它。 code正常工作在FF

jQuery的问题

$。阿贾克斯({     键入:GET,     网址:设置presentationLocation,     数据类型:XML,     异步:假的,     的contentType:应用程序/ XML,     成功:函数(XML){         //设置幻灯片         $(XM​​L).find('滑')。每个(函数(){             //创建幻灯片             obj.append('< D​​IV CLASS =幻灯片>< D​​IV CLASS =slideTitle>'+ $(本).find(标题)文本()+'< / DIV>&LT ; DIV CLASS =slideContent>'+ $(本).find(内容)。文字()+'< / DIV>< / DIV>');         });         totalSlides = obj.children('滑')的大小()。         //隐藏所有的幻灯片         obj.children('滑')隐藏()。     },     错误:函数(xmlReq,状态,ERRORMSG){         的console.log(错误消息:+ ERRORMSG);         的console.log(状态:+状态);         执行console.log(xmlReq.responseText);         扔(ERRORMSG);     } });

XML文件

< XML版本=1.0编码=UTF-8&GT?; <幻灯片>     <滑动>         <冠军>幻灯第3版; /标题>         <内容>您好< /内容>     < /幻灯片> < /幻灯片>

解决方案

不是一个理想的解决方案,但它的工作原理:

我很快发现,我不是有这个问题只有一个:

谷歌搜索, JQuery的错误,Stackoverflow问题

和一切我似乎读点IE浏览器如何读取并解析XML。找到一个聪明的解决方案,在这里阅读注释:

博客看到评论#28

这仍然没有奏效。经过一番摆弄AJAX功能小有一点我发现,如果我删除的数据类型,除了在博客文章的评论#28,一切都跨浏览器的工作。

最后code是这样的:

//获得我们的文档 $阿贾克斯({     键入:GET,     异步:假的,     网址:设置presentationLocation,     成功:函数(结果){         VAR XML = methods.parseXML(结果);         $(XM​​L).find('滑')。每个(函数(){             //创建幻灯片             obj.append('< D​​IV CLASS =幻灯片>< D​​IV CLASS =slideTitle>'+ $(本).find(标题)文本()+'< / DIV>&LT ; DIV CLASS =slideContent>'+ $(本).find(内容)。文字()+'< / DIV>< / DIV>');         });         totalSlides = obj.children('滑')的大小()。         //隐藏所有的幻灯片         obj.children('滑')隐藏()。     },     错误:函数(xmlReq,状态,ERRORMSG){         VAR ERRMSG =设置presentationLocation +:+ ERRORMSG;         扔(ERRMSG);     } });

在这里methods.parseXML被定义为

parseXML:功能(XMLFILE){     如果(window.ActiveXObject){         // IE         VAR DOC =新的ActiveXObject(Microsoft.XMLDOM);         doc.loadXML(XMLFILE);         返回文档;     }否则,如果(window.DOMParser){         返回(新的DOMParser).parseFromString(XMLFILE,为text / xml');     }     返回 ; }

In IE, JQuery is giving me a parseError when I'm trying to read a local XML file. Hoping someone might be able to spot it. Code works fine in FF

Jquery in question

$.ajax({ type: "GET", url: settings.PresentationLocation, dataType: "xml", async: false, contentType : 'application/xml', success: function(xml){ //Setup the slides $(xml).find('slide').each(function(){ //Create the slide obj.append('<div class="slide"><div class="slideTitle">'+ $(this).find('title').text() +'</div><div class="slideContent">'+ $(this).find('content').text() +'</div></div>'); }); totalSlides = obj.children('.slide').size(); //Hide all the slides obj.children('.slide').hide(); }, error: function(xmlReq, status, errorMsg){ console.log("Error Message: "+ errorMsg); console.log("Status: "+ status); console.log(xmlReq.responseText); throw(errorMsg); } });

XML File

<?xml version="1.0" encoding="UTF-8"?> <slides> <slide> <title>Slide 3</title> <content>Hi there</content> </slide> </slides>

解决方案

Not an Ideal Solution but it works:

I quickly found out that I am not the only one having this problem:

google search , JQuery Bug , Stackoverflow question

and everything I seem to read points to how IE reads and parses XML. Found a clever solution reading the comments here:

blog see comment #28

This still didn't work. After some playing with the ajax function alittle I found that if I removed the dataType, in addition to the comment #28 in the blog post, everything worked across browsers.

Final code looks like:

//Retrieve our document $.ajax({ type: "GET", async: false, url: settings.PresentationLocation, success:function(results){ var xml = methods.parseXML(results); $(xml).find('slide').each(function(){ //Create the slide obj.append('<div class="slide"><div class="slideTitle">'+ $(this).find('title').text() +'</div><div class="slideContent">'+ $(this).find('content').text() +'</div></div>'); }); totalSlides = obj.children('.slide').size(); //Hide all the slides obj.children('.slide').hide(); }, error: function(xmlReq, status, errorMsg){ var errMsg = settings.PresentationLocation + " : "+ errorMsg ; throw(errMsg); } });

where methods.parseXML is defined as

parseXML : function(xmlFile){ if (window.ActiveXObject) { //IE var doc = new ActiveXObject('Microsoft.XMLDOM'); doc.loadXML(xmlFile); return doc; } else if (window.DOMParser) { return (new DOMParser).parseFromString(xmlFile, 'text/xml'); } return ""; }

更多推荐

对本地XML文件IE JQuery的AJAX parseerror

本文发布于:2023-10-30 03:32:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1541641.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   XML   JQuery   parseerror   AJAX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!