2014-01-26 99 views
3

我嘗試解析通過AJAX一個Google Alert飼料,但我得到的例外。這是我的嘗試:解析谷歌警報飼料

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

,但我得到:

XMLHttpRequest cannot load http://www.google.com/alerts/feeds/01662123773360489091/16526224428036307178. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. 

任何幫助嗎?


P.S:我已經看過幾個職位有關分析RSS通過AJAX和jQuery飼料,但他們沒有爲我工作。

+0

看起來像一個跨域問題。 [這個網站](http://cypressnorth.com/programming/cross-domain-ajax-request-with-xml-response-for-iefirefoxchrome-safari-jquery/)有一些方法的說明。 –

回答

3

對不起,你將不會被允許使用跨域腳本。查看答案在這裏:

jQuery XML REST Access-Control-Allow-Origin

它只要是不可能的,因爲REST API(不是你的 服務器)的服務器允許來自不同來源的請求,通過設置CORS (跨源資源共享)HTTP頭中,例如通過設置在 「訪問控制允許來源」 HTTP頭中的響應:

訪問控制允許來源:*

當谷歌的頭看,它們不提供該選項:

$ curl -I 'http://www.google.com/alerts/feeds/01662123773360489091/16526224428036307178' 
HTTP/1.1 200 OK 
Date: Sun, 26 Jan 2014 23:46:50 GMT 
Pragma: no-cache 
Expires: Fri, 01 Jan 1990 00:00:00 GMT 
Cache-Control: no-cache, must-revalidate 
Content-Type: text/xml; charset=UTF-8 
X-Frame-Options: DENY 
Set-Cookie: PREF=ID=3c2ff90717c79524:TM=1390780010:LM=1390780010:S=f9cyzI-rk2Nca7W3; expires=Tue, 26-Jan-2016 23:46:50 GMT; path=/; domain=.google.com 
X-Content-Type-Options: nosniff 
Server: psfe 
X-XSS-Protection: 1; mode=block 
Alternate-Protocol: 80:quic 
Transfer-Encoding: chunked 

你唯一的選擇就是使用你的服務器來獲取值,則中繼值到你的jQuery應用程序。

3

,你說的對,http://www.google.com/alerts/feeds/...沒有發回一個響應頭叫Access-Control-Allow-Origin服務器。因爲它沒有發回這個頭讓你的網站聯繫它,你的瀏覽器正在阻止這些請求。這是瀏覽器實施的安全措施。如果你在做這個服務器端,不存在這樣的限制。但客戶端,它有所作爲,這些限制已經到位。

您需要創建一個服務器端代理(在C#編寫/ Python的/你的選擇)這反過來又使你的目標URL的請求。

換句話說,你JQuery的要求http://example.com/proxy/alerts.py這反過來又調用谷歌快訊的網址,並將其發送給JavaScript。

0

你明白複製代碼從answer here on SO
當然,如果沒有至少試圖從XML中獲取確切的名字,它就無法工作。 enclosure不在該供稿結果中。 PHP的

一號線和AJAX調用到PHP文件:

getFeed.php

<?php echo file_get_contents(htmlspecialchars($_GET['url'])); ?> 

yourpage。html

<!DOCTYPE html> 
<html> 
<head> 
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script> 
<meta charset=utf-8 /> 
<title>Test Feeds by Roko C.B.</title> 
</head> 
<body> 

<p id="feeds"></p> 

<script> 
$(function(){ 

    var feedsHTML = "" 

    $("<div/>").load('getFeed.php?url=http://www.google.com/alerts/feeds/01662123773360489091/16526224428036307178', function(response, status, xhr){ 
     var xmlDoc = $.parseXML(response), 
      $xml = $(xmlDoc), 
      $entry = $xml.find("entry"); 
     $.each($entry, function(i){ 
      var title = $(this).find('title').text(); 
      var link = $(this).find('link').attr("href"); 
      feedsHTML += "<a href='"+ link +"'>"+ title +"</a> <br>"; 
     }); 
     $('#feeds').html(feedsHTML); 
    }); 

});  
</script> 

</body> 
</html> 

概念驗證?這是我得到的結果:

<p id="feeds"><a href="https://www.google.com/url?q=http://www.arabnews.com/news/516076&amp;ct=ga&amp;cd=&amp;cad=CAI&amp;usg=AFQjCNFhY_O6hwv7_d9EaA0pIyxFBstrlQ">Premarital drug <b>test</b> 'useless'</a> <br><a href="https://www.google.com/url?q=http://secondgradesugarandspice.blogspot.com/2014/01/ready-to-write-test-prep-practice-for.html&amp;ct=ga&amp;cd=&amp;cad=CAI&amp;usg=AFQjCNH3y_Bf6rk_sPoshh6F5b45308daQ">Sugar and Spice: Ready to Write! <b>Test</b> Prep &amp; Practice For Lil <b>...</b></a> <br><a href="https://www.google.com/url?q=http://www.csmonitor.com/Commentary/the-monitors-view/2014/0126/Ukraine-protests-a-test-for-the-idea-of-progress-as-inevitable&amp;ct=ga&amp;cd=&amp;cad=CAI&amp;usg=AFQjCNG9jCY57FsoCsrOZcP_timbk7odVg">Ukraine protests: a <b>test</b> for the idea of progress as inevitable</a> <br><a href="https://www.google.com/url?q=http://tommorris.org/posts/8757&amp;ct=ga&amp;cd=&amp;cad=CAI&amp;usg=AFQjCNGdE5yyKMLFDlPA-qfq5C_aMn7O3g">The Minimal Viable Vegetarian <b>test</b> - Tom Morris</a> <br><a href="https://www.google.com/url?q=http://www.navy-net.co.uk/joining-up-royal-navy-recruiting/73629-test.html&amp;ct=ga&amp;cd=&amp;cad=CAI&amp;usg=AFQjCNG4-3ZfVeGj1jA7FCBgQsD5jO9lwA"><b>Test</b> - Navy Net</a> <br><a href="https://www.google.com/url?q=http://www.rawstory.com/rs/2014/01/26/dna-test-of-7000-year-old-tooth-overturns-popular-image-of-light-skinned-european-hunter-gatherers/&amp;ct=ga&amp;cd=&amp;cad=CAI&amp;usg=AFQjCNGjqu6y8GTcw1W_MFzen2nR0mBkIQ">DNA <b>test</b> of 7000-year-old tooth overturns popular image of light <b>...</b></a> <br><a href="https://www.google.com/url?q=http://financialadvisercoach.com/2014/01/27/the-4-step-ethics-test-for-financial-professionals/&amp;ct=ga&amp;cd=&amp;cad=CAI&amp;usg=AFQjCNHn-o8toDuYi-PX4buRf0hZCfy4cw">The 4 Step Ethics <b>Test</b> for Financial Professionals – The Financial <b>...</b></a> <br><a href="https://www.google.com/url?q=http://nypost.com/2014/01/26/test-tube-grown-meat-might-be-coming-to-brooklyn/&amp;ct=ga&amp;cd=&amp;cad=CAI&amp;usg=AFQjCNHd8-9mUEGl_VwJXkLDjInWQsGYTA"><b>Test</b> tube grown meat might be coming to Brooklyn</a> <br><a href="https://www.google.com/url?q=http://www.ministryoftesting.com/2014/01/testing-presentations/&amp;ct=ga&amp;cd=&amp;cad=CAI&amp;usg=AFQjCNEfjw4Gr3Io70EVzZCx0lZjq7WOrg"><b>Testing</b> Presentations | Ministry of TestingMinistry of <b>Testing</b></a> <br><a href="https://www.google.com/url?q=http://gospeldrivendisciples.blogspot.com/2014/01/the-test-of-fellowship-part-3.html&amp;ct=ga&amp;cd=&amp;cad=CAI&amp;usg=AFQjCNHX5Q5kZfeHSTo389RAVutxxwHWcg">*Gospel-driven Disciples: The <b>Test</b> of Fellowship (Part 3)</a> <br><a href="https://www.google.com/url?q=http://forums.wpcentral.com/developers-beta-testing/260700-test-livezen-windows-phone.html&amp;ct=ga&amp;cd=&amp;cad=CAI&amp;usg=AFQjCNFjJuCwnVm_FGFbhrnQFuEG54bi2w"><b>Test</b> LiveZen on Windows Phone - Windows Phone Central Forums</a> <br><a href="https://www.google.com/url?q=http://www.clevelandbanner.com/view/full_story/24452083/article-CSCC-to-offer-both-HiSet--GED-tests%3Finstance%3Dlatest_articles&amp;ct=ga&amp;cd=&amp;cad=CAI&amp;usg=AFQjCNEv00h7ou07AOehSP2l16Wmdv-h_Q">CSCC to offer both HiSet, GED <b>tests</b></a> <br><a href="https://www.google.com/url?q=http://www.willitsnews.com/news/ci_24996241/api-school-test-scores-expected-vanish-california-2&amp;ct=ga&amp;cd=&amp;cad=CAI&amp;usg=AFQjCNFyKOmfO9R_E9VYbA6xkYnVmwIO6w">API school <b>test</b> scores expected to vanish in California for 2 years <b>...</b></a> <br><a href="https://www.google.com/url?q=http://www.farmweekly.com.au/news/agriculture/cattle/dairy/preg-test-lifts-dairy-profit/2685288.aspx&amp;ct=ga&amp;cd=&amp;cad=CAI&amp;usg=AFQjCNFFCyCOwYkmo-BmaW44cEXclb8VqA">Preg <b>test</b> lifts dairy profit</a> <br><a href="https://www.google.com/url?q=http://ajw.asahi.com/article/economy/technology/AJ201401270001&amp;ct=ga&amp;cd=&amp;cad=CAI&amp;usg=AFQjCNGXyAR1NLEgPti_Hfm8MJCETjvZtg">Researchers <b>test</b> sail for energy-efficient cargo ship</a> <br><a href="https://www.google.com/url?q=http://bringatrailer.com/2014/01/25/one-of-one-1970-chrysler-electric-test-vehicle-1/&amp;ct=ga&amp;cd=&amp;cad=CAI&amp;usg=AFQjCNHgALMWfD8AHa87pDRzcGGtTFXf6A">One of One: 1970 Chrysler Electric <b>Test</b> Vehicle-1 - Bring a Trailer</a> <br><a href="https://www.google.com/url?q=http://forums.linn.co.uk/bb/showthread.php%3Ftid%3D25860&amp;ct=ga&amp;cd=&amp;cad=CAI&amp;usg=AFQjCNGkR33R46DtOCfuwrqd5wBCC6BvDw">Interesting Hearing Study and <b>Test</b> - Linn Forums</a> <br><a href="https://www.google.com/url?q=http://www.musiccitymiracles.com/2014/1/26/5347204/tennessee-titans-free-agents-sign-test-cut&amp;ct=ga&amp;cd=&amp;cad=CAI&amp;usg=AFQjCNEglk6qoddYkY_H6ISPAyRzteCJ5w">Tennessee Titans free agents: Sign, <b>test</b>, cut - Music City Miracles</a> <br><a href="https://www.google.com/url?q=http://techlorebyigor.blogspot.com/2014/01/passing-my-piss-test.html&amp;ct=ga&amp;cd=&amp;cad=CAI&amp;usg=AFQjCNGFXpQSZbXZL2oKANeYbWA1q2_eng">tech lore by igor: Passing My Piss-<b>Test</b></a> <br><a href="https://www.google.com/url?q=http://www.lenorahenson.com/2014/01/test.html&amp;ct=ga&amp;cd=&amp;cad=CAI&amp;usg=AFQjCNEqPuP8KuzAngKU5jO43pKtK1dahA">The Lenora Aura: <b>test</b></a> <br></p>