2012-03-19 60 views
0

我正在通过jquery ajax解析一些xml。除了IE之外,我的所有浏览器都有很好的工作。与IE浏览器的Jquery Ajax

我已经缩小到IE挂起的位置,我不知道如何解决它。我不知道是否这是我用来获取html的jquery的问题,或者如果它是我的xml的问题,或者它是我忽略的东西。有趣的是,当我提醒我的变量持有xml数据时,IE会正确地警告所有信息,但它似乎无法正确地遍历它并格式化数据。请帮忙。

这里是我使用从我的XML提取数据的代码:

function parseXML (data) { 
     $(data).find("section").each(function() { 
      var $section = $(this), 
       photos = $section.find("photo"), 
       photoContainer = $("<div></div>", { 
        id: $section.attr("id"), 
        "class": "gallery-section" 
       }); 

      photos.each(function() { 

       var photo = $(this), 
        imageurl = photo.attr("imageurl"), 
        title = photo.find("title").text(), 
        description = photo.find("description").html(), 
        kind = photo.find("description").attr("type"); 
       icon = photo.find("icon").attr("source"); 
       iconClass = photo.find("icon").attr("class"); 

       var photoWrapper = $("<div class='photo'></div>"), 
        imageElem = $("<img />", { 
         "src": imageurl, 
         "class": "gallery-photo" 
        }), 
        photoInfo = $("<div></div>", { 
         "class": "photo-info " + kind 
        }), 
        iconInsert = $("<img />", { 
         "src": icon, 
         "class": iconClass 
        }), 
        header = $("<h1></h1>", { 
         text: title 
        }), 
        photoDescription = $("<div></div>", { 
         html: description 
        }); 

       photoInfo.append(iconInsert).append(header).append(photoDescription); 
       photoWrapper.append(imageElem).append(photoInfo); 
       photoContainer.append(photoWrapper); 

      }); 
      $("#photo-viewer-inner").append(photoContainer); 
     }); 
     var videos = "<div id='videos'></div>"; 
     $("#photo-viewer-inner").append(videos); 
     $("#videos").load("images/gallery-images/videos.html #video-inner"); 
    } 

/* Get Photos From XML */ 
var dataType; 
if ($.browser.msie) { 
    dataType = "text"; 
} else { 
    dataType = "html"; 
} 

$.ajax({ 
    type: "GET", 
    url: "images/gallery-images/gallery-images.xml", 
    dataType: dataType, 
    success: function(data, status) 
{ 
    parseXML(data); 
    alert(data); 
}, 
}); 
+0

IE悬挂在哪里?而且,你是否尝试过'dataType:xml'? – 2012-03-19 21:19:05

+0

IE到达函数'parseXML'的第2行时挂起。它似乎得到的XML文件的内容很好,但它不能遍历它们并提取我需要的数据。是的,我试着用多种不同的组合来改变dataTypes和contentTypes。没有这样的运气。 – jcbfshr 2012-03-19 21:23:04

+0

不能使用“class”作为var或属性名称,大多数浏览器接受它,但不应该。 – Dampsquid 2012-03-19 21:25:55

回答

0

IE的JavaScript不允许被用作一个变量或属性名称,而其他browzers做。这个链接有一些互动评论bugzilla.mozilla,看起来像IE是正确的一次。

+0

所以,我的图标在我的xml中的“类”属性扔这一切?或者还有什么我需要改变的? – jcbfshr 2012-03-19 21:50:15

+0

在JS代码 – Dampsquid 2012-03-19 21:52:00

+0

中没有像这样的“class”:“gallery-section”的xml东西哇。好。我想我会开始试图改变这个东西,然后让它工作。有什么帮助吗? – jcbfshr 2012-03-19 22:08:18

0

在您的AJAX调用从函数删除尾随逗号。

更改此:

$.ajax({ 
    type: "GET", 
    url: "images/gallery-images/gallery-images.xml", 
    dataType: dataType, 
    success: function(data, status) 
    { 
     parseXML(data); 
     alert(data); 
    }, 
}); 

这样:

$.ajax({ 
    type: "GET", 
    url: "images/gallery-images/gallery-images.xml", 
    dataType: dataType, 
    success: function(data, status) 
    { 
     parseXML(data); 
     alert(data); 
    } 
}); 

引用另一SO交:

“虽然创建对象{}或数组[], 你用逗号分隔单独的元素,但这里有一个 附加逗号后的最后一项像[a,b, c,] - 不是根据ECMA-262允许的 。“

+0

检查此链接的参考:http://stackoverflow.com/questions/1819821/trailing-comma-problem-javascript – 2012-03-19 21:57:08