weixin_33749131 2017-11-26 17:13 采纳率: 0%
浏览 26

使用AJAX解析XML

Having trouble parsig xmlusing ajax. Can't seem to see what's going wrong here, as I have been able to retrieve API's using ajaxelsewhere in my code. Any help would be greatly appreciated.

html:

<div id="technology" class="tab-pane fade">
  <h3>TECH</h3>
  <p id="tech_news"></p>
</div>

jquery:

$(document).ready(function() {
  $.ajax({
    type: "GET",
    url: "http://feeds.bbci.co.uk/news/technology/rss.xml",
    dataType: "xml",
    cache: false,
    success: parseXml
  });
});

function parseXml(xml) {
  $(xml).find("item").each(function() {
    $("#tech_news").append($(this).find("title").text() + "<p>");
    $("#tech_news").append($(this).find("description").text() + "<p>");
  });
}
  • 写回答

1条回答 默认 最新

  • weixin_33701564 2017-11-26 18:09
    关注

    Actually you have a corsissue. We have this error message:

    Failed to load http://feeds.bbci.co.uk/news/technology/rss.xml?_=1511718738881: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

    However you can use this service https://crossorigin.me/ to allow cross origins requests through ajax.

    Something like this:

    $(document).ready(function() {
      $.ajax({
        type: "GET",
        url: "https://crossorigin.me/http://feeds.bbci.co.uk/news/technology/rss.xml",
        dataType: "xml",
        cache: false,
        success: parseXml
      });
    });
    
    function parseXml(xml) {
      $(xml).find("item").each(function() {
        $("#tech_news").append($(this).find("title").text() + "<p>");
        $("#tech_news").append($(this).find("description").text() + "<p>");
      });
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <div id="technology" class="tab-pane fade">
      <h3>TECH</h3>
      <p id="tech_news"></p>
    </div>

    Even so, you should know that it is an external service that may not be operational 24/7.

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题