jquery / ajax feed解析器脚本无法在页面上工作

时间:2016-12-07 02:39:23

标签: javascript jquery xml rss

  $(document).ready(function() {
  $(function () {
      url = 'http://feeds.bbci.co.uk/news/world/us_and_canada/rss.xml?fmt=xml';
      $.ajax({
          type: "GET",
          url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=1000&callback=?&q=' + encodeURIComponent(url),
          dataType: 'json',
          error: function () {
              alert('Unable to load feed, Incorrect path or invalid feed');
          },
          success: function (xml) {
              values = xml.responseData.feed.entries;
              $.each(values, function (i) {
                  $('#results').append('<h1>' + values[i].title + '</h1>');
                  $('#results').append(values[i].content);
              });

          }
      });
  });
});

上面的代码在我的jsfiddle中运行正常 - 但实施时它始终存在错误alert()函数

我的控制台报告: (报告以下本地和实时服务器)

XMLHttpRequest无法加载http://feeds.bbci.co.uk/news/world/us_and_canada/rss.xml?format=xml。 No&#39; Access-Control-Allow-Origin&#39;标头出现在请求的资源上。起源&#39; null&#39;因此不允许访问。 home.html:1 XMLHttpRequest无法加载http://bsnm.s3.amazonaws.com/IVC/103b8b081e0c4ee0ef0d57d45ed11104。 No&#39; Access-Control-Allow-Origin&#39;标头出现在请求的资源上。起源&#39; null&#39;因此不允许访问。

2 个答案:

答案 0 :(得分:0)

我能够使用jQuery v1.1.1在我的本地盒子上重现这个bug,但是当我尝试使用jQuery v2.1.1时它会正常加载。

您使用的是旧版jQuery吗?

注意:我在本地测试的代码与你的jsFiddle完全相同,只不过我改了 document.location.protocol'http://' 并添加了<script src="https://code.jquery.com/jquery-2.1.1.js"></script>

答案 1 :(得分:0)

出于某些奇怪的原因 - 问题是:

url: document.location.protocol + 

当我更换&#39; document.location.protocol&#39;实际http:它可以正常工作。

我正在加载jQuery v 3.1 - 不确定这是否重要。如果有人对为什么或其原因有所了解,请加我的答案!

ie。现在可以正常工作。

url: 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=1000&callback=?&q=' + encodeURIComponent(url),
相关问题