对本地XML文件IE JQuery的AJAX parseerror [英] IE JQuery ajax parseerror on local xml file

查看:127
本文介绍了对本地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版; /标题>
        <内容>您好< /内容>
    < /幻灯片>
< /幻灯片>
 

解决方案

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

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

<一个href="http://www.google.com/search?q=Jquery+XML+parseerror&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla%3aen-US%3aofficial&client=firefox-a"相对=nofollow>谷歌搜索, JQuery的错误,<一个href="http://stackoverflow.com/questions/2011076/parseerror-when-getting-xml-via-jquery-ajax">Stackoverflow问题

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

博客看到评论#28

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

最后code是这样的:

  //获得我们的文档
$阿贾克斯({
    键入:GET,
    异步:假的,
    网址:设置presentationLocation,
    成功:函数(结果){
        VAR XML = methods.parseXML(结果);

        $(XM​​L).find('滑')。每个(函数(){
            //创建幻灯片
            obj.append('&LT; D​​IV CLASS =幻灯片&GT;&LT; D​​IV CLASS =slideTitle&GT;'+ $(本).find(标题)文本()+'&LT; / DIV&GT;&LT ; DIV CLASS =slideContent&GT;'+ $(本).find(内容)。文字()+'&LT; / DIV&GT;&LT; / DIV&GT;');
        });

        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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆