开发者

jQuery XML parsing in IE8

开发者 https://www.devze.com 2023-01-14 11:41 出处:网络
I have some XML that I am parsing in jQuery. <payload> 开发者_运维知识库<value key=\"VehicleMake\">

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.


I don't think you want a space here:

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消