jQuery XML parsing in IE8

独自空忆成欢 提交于 2019-12-24 09:48:39

问题


I have some XML that I am parsing in jQuery.

<payload>
    <value key="VehicleMake">
        <value key="description">Aeon</value>
        <value key="code">18</value>
    </value>
    <value key="VehicleMake">
        <value key="description">Alfa Romeo</value>

        <value key="code">120</value>
    </value>
</payload>

In all browsers except IE (specifically IE8) the following works:

$.ajax({
    type : "GET",
    async : false,
    url : "/services/vehiclemake",
    success :
        function(xmlResponse) {
        var data = $("payload", xmlResponse).children("value").map(function() {     
            //IE8 fails on the next line
            var code = $(this).children("value [key='code']").text();
            var desc = $(this).children("value [key='description']").text();
            return {
                value : desc,
                vehicleCode  : code,
                label : desc
            };
        }).get();
    }
});

In IE8 it fails on the assignment to 'code' saying : "TypeError: Object doesn't support this property or method".

If I use this approach then it is fine:

$.ajax({
    type : "GET",
    async : false,
    url : "/services/vehiclemake",
    success :
        function(xmlResponse) {
        var data = $("payload", xmlResponse).children("value").map(function() {
            var code;
            var desc;           
            var elements = $(this).children();
            for (i = 0; i < elements.length; i++) {
                if(elements[i].attributes.getNamedItem("key").value == 'code') {
                    code = elements[i].text;
                }
                if(elements[i].attributes.getNamedItem("key").value == 'description') {
                    desc = elements[i].text;
                }
            }
            var desc = $(this).children("value [key='description']").text();
            return {
                value : desc,
                vehicleCode  : code,
                label : desc
            };
        }).get();

Can anyone tell me why the standard jQuery attribute selectors are not working in IE8? The XML is being returned with the correct mime type.

Thanks.


回答1:


I don't think you want a space here:

.children("value[key='code']")

Might not solve it but I think that needs fixing anyway.



来源:https://stackoverflow.com/questions/3606778/jquery-xml-parsing-in-ie8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!