dongshi9526 2014-02-01 21:05
浏览 32
已采纳

从ajax请求中提取内容

I am trying to extract content from ajax request. I am able to extract some information but am having problem with others.

Below is an example of some of the requested information:

<item>
<title>Title</title>
<pubDate>Sat, 01 Feb 2014 12:12:12 GMT</pubDate>
<description>Description</description>
<enclosure url="http://www.podtrac.com/pts/redirect.mp3/traffic.libsyn.com/example.mp3" type="audio/mpeg" />
<itunes:duration>1:35:25</itunes:duration>
</item>

The Desired output:

Title
Sat, 01 Feb 2014 12:12:12 GMT
Description
1:35:25
http://www.podtrac.com/pts/redirect.mp3/traffic.libsyn.com/example.mp3

So far I have been able to extract the title, pubDate and description. I have been unsuccessful with the url and duration. I have tried different methods but have been unsucessful. I hope you can help me find a solution and thank you for your time.

Below is my code:

$.ajax({
type: "GET",
url: "php/podcast.php",
dataType: "xml",
success: function(xml){
$(xml).find('item').each(function(){
var sTitle = $(this).find('title').text();
var sPubDate = $(this).children('pubDate').text();
var sDescription = $(this).find('description').text();
var sDuration = $(this).find('itunes:duration').text();
var sURL = $(this).text().match('podtrac');
$('#rtpodcast').append('<div id="podcast"><div id="podTitle">'+ sTitle +'</div><div        id="podPubDate">'+ sPubDate +'</div><div id="podDescription">'+ sDescription +'</div><div  id="podDuration">'+ sDuration +'</div><div id="podURL">'+ sURL +'</div></div>');
});
},
error: function() {
alert("An error occurred while processing XML file.");
}
});
  • 写回答

1条回答 默认 最新

  • duang5049 2014-02-01 21:37
    关注

    This will match the text between the tags and not attributes.

    example(Fiddle):

    var xml = $.parseXML('<a id="text_id">text</a>');
    alert($(xml).find('a').text()); // this will alert "text"
    alert($(xml).find('a').attr("id")); //this will alert "text_id"
    

    To retrive url you need to replace this

    var sURL = $(this).text().match('podtrac');
    

    with

    var sURL = $(this).find("enclosure").attr("url");
    

    For tag with namespace, try escaping colon with // in your selector

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测