jQuery xml parse with check for IE8 not working

2019-08-05 08:11发布

I have looked through and tried all the SO answers on jQuery, XML and IE8, but nothing is giving me a result. Any other suggestions on how to get this to working with jQuery or Javascript?

  $.ajax({
        type: "GET",
        url: "http://query.yahooapis.com/v1/public/yql?q=select%20title%2C%20link%20from%20rss%20where%20url%3D%22http%3A%2F%2Fwww.fs.usda.gov%2Fwps%2FPA_WIDConsumption%2Frssgetfile%3FxFSENavChannel00%3D1109%26xFSENavChannel02%3D091000000000000%26pathinfo%3D%2Fwps%2Fportal%2Ffsinternet%2F!ut%2Fp%2Fc5%2F04_SB8K8xLLM9MSSzPy8xBz9CP0os3gjAwhwtDDw9_AI8zPwhQoYAOUjzeINcABHAwK6w0H2YaoI84GrAMvjMd_PIz83Vb8gN8Igy8RREQC1scDO%2Fdl3%2Fd3%2FL2dJQSEvUUt3QS9ZQnZ3LzZfMjAwMDAwMDBBODBPSEhWTjJNMDAwMDAwMDA!%2F%3Fnavtype%3DBROWSEBYSUBJECT%26forestname%3DRegion%25209%22",
    dataType:"xml",    
    success: function(msg){             
            data = parseXml(msg);

            $(data).find('item').each(function(index,val){
                var title = $(this).find("title").text();
                var link = $(this).find("link").text();
                $('#loadnews').append("<li><a href="+link+">"+title+"</a></li>");
            });    
        }
    });     

function parseXml(xml) {
     if ($.browser.msie  && parseInt($.browser.version, 10) === 8) {
        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.loadXML(xml);
        xml = xmlDoc;
    }   
    return xml;
}
if ($.browser.msie  && parseInt($.browser.version, 10) === 8) {
  alert('IE8');
} else {
  alert('Non IE8');
}

Fiddle is here: http://jsfiddle.net/tjbLf/7/

1条回答
Ridiculous、
2楼-- · 2019-08-05 09:10

Why do you need XML transformation? jQuery can do all the job for you:

$.ajax({
    type: "GET",
    url: "...",
    dataType: "xml",
    success: function(data) {
        $(data).find('item').each(function(index, val) {
            var title = $(this).find("title").text();
            var link = $(this).find("link").text();
            $('#loadnews').append("<li><a href=" + link + ">" + title + "</a></li>");
        });
    }
});​

DEMO: http://jsfiddle.net/tjbLf/9/

However, in cross-domain requests IE will give "No transport" error. Consider using JSONP to receive the data from external web services.

查看更多
登录 后发表回答