2016-12-07 112 views
3
$(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()功能的jQuery/AJAX飼料解析器腳本

我的控制檯報告:(報告下面的本地和現場服務器)

XMLHttpRequest無法加載http://feeds.bbci.co.uk/news/world/us_and_canada/rss.xml?format=xml。請求的資源上沒有「Access-Control-Allow-Origin」標題。因此不允許原產地'null'訪問。 home.html:1 XMLHttpRequest無法加載http://bsnm.s3.amazonaws.com/IVC/103b8b081e0c4ee0ef0d57d45ed11104。請求的資源上沒有「Access-Control-Allow-Origin」標題。因此不允許原產地'null'訪問。

+0

你可以給錯誤函數添加一個'err'參數並提醒'err.message'嗎? –

+1

是'document.location.protocol'' https:'? – guest271314

回答

0

對於一些奇怪的原因 - 這個問題是這樣的:

url: document.location.protocol + 

當我用實際的http:替換'document.location.protocol'時,它工作正常。

我正在加載jQuery v 3.1 - 不確定是否有問題。如果有人有洞察力爲什麼或者原因爲此,請加我的答案!

即,下面的現在工作。

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

我能夠使用jQuery v1.1.1在本地盒子上重現此錯誤,但在我嘗試jQuery v2.1.1時加載正常。

您使用的是舊版本的jQuery嗎?

注:我在本地測試的代碼是完全一樣的你的jsfiddle除非我改變 document.location.protocol'http://' 並添加<script src="https://code.jquery.com/jquery-2.1.1.js"></script>