解析使用jQuery和Ajax XMLjQuery、Ajax、XML

2023-09-11 00:36:43 作者:雪亱dě白狐

我有一个RSS feed我的播客,基本上我所要做的是填充一个HTML5的音频播放器,RSS提要中的URL。

I have an rss feed for my podcast and essentially what I am trying to do is populate an html5 audio player with URLs within the RSS feed.

我到哪去这个是用ajax解析的链接,然后将它们附加到音频播放器的src的最佳途径。我知道同一个域策略,将prevent我这样做与阿贾克斯的,所以我使用的跨域AJAX插件(http://bit.ly/Jbi9iX)来解决这个问题。

I figure the best way to go about this is to parse the links using ajax and then append them into the src of the audio player. I am aware of the same domain policy that would prevent me doing this with ajax, so I am using the cross domain ajax plugin (http://bit.ly/Jbi9iX) to get around this.

我在努力弄清楚到底为什么code以下工作不适合我,基本上在这个阶段,我只是想追加网​​址的内RSS源进入 #results 来表明它的工作,那么我将它添加到的的的src 部分音频播放器。

I am struggling to figure out exactly why the code below is not working for me, basically at this stage I simply want to append the url's within the RSS feed into the #results to show that its working, then I will add it to the src part of the audio player.

$(document).ready(function () {
    $.ajax({
        url: 'http://theresidency.libsyn.com/rss',
        type: 'GET',
        dataType: "xml",
        success: parseXml
    });
});

function parseXml(xml) {
var item = $(xml).find("item");

  $(item).each(function() {
    $("#results").append($("enclosure").attr("url").text() + "<br />");
  });

}

我没有收到在Chrome开发工具的任何错误,我已经看了看周围其他的例子,但我可以找出我做错了。

I am not getting any errors in chrome dev tools, and I have looked around at other examples but I can figure out what I'm doing wrong.

下面是XML / RSS的例子: http://pastebin.com/stuY495c 这里是我迄今上传: http://bit.ly/J9QHZc

Here is an example of the xml/rss: http://pastebin.com/stuY495c And here is what I have so far uploaded: http://bit.ly/J9QHZc

任何帮助将是提前pciated这么多的感谢AP $ P $!

Any help would be much appreciated so thanks in advance!

推荐答案

确切位置在哪里,你传递数据的功能,我想你需要做的:

Where exactly are you passing the data to the function, I think you need to do:

$(document).ready(function () {
    $.ajax({
        url: 'http://theresidency.libsyn.com/rss',
        type: 'GET',
        dataType: "xml",
        success: function(data) {
           parseXml(data);
        }
    });
});

function parseXml(xml) {
var item = $(xml).find("item");

  $(item).each(function() {
    $("#results").append($("enclosure").attr("url").text() + "<br />");
  });

}

或者只是:

$(document).ready(function () {
    $.ajax({
        url: 'http://theresidency.libsyn.com/rss',
        type: 'GET',
        dataType: "xml"
    }).done(function(xml) {
        $.each($("item", xml), function(i, e) {
            $("#results").append($("enclosure").attr("url").text() + "<br />");
        });
    });
});

编辑:

做了一些更多与此摆弄,并提出了:

Did some more fiddling with this, and came up with:

$(document).ready(function () {
    $.ajax({
        url: 'http://query.yahooapis.com/v1/public/yql?q=%20SELECT%20*%20FROM%20xml%20WHERE%20url%3D%22http%3A%2F%2Ftheresidency.libsyn.com%2Frss%22&format=json&callback=',
        dataType: "json"
    }).done(function(data) {
        $.each(data.query.results.rss.channel.item, function() {
            $("#results").append(this.enclosure.url + "<br />");
        });
    });
});​

我相信这是你在找什么,这里有一个 演示

 
精彩推荐
图片推荐