3

我尝试Google Alert通过 ajax 解析提要,但出现异常。这是我尝试过的:

$(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. 

有什么帮助吗?


PS:我已经阅读了几篇关于通过 ajax 和 jquery 解析 rss 提要的文章,但没有一篇对我有用。

4

3 回答 3

3

The server that you are talking to, http://www.google.com/alerts/feeds/... isn't sending back a response header called Access-Control-Allow-Origin. Because it's not sending this header back allowing your site to contact it, your browser is blocking these requests. This is a security measure that browsers implement. No such restriction exists if you were doing this server side. But client-side, it makes a difference and these restrictions are in place.

You will need to create a server side proxy (written in C#/Python/your choice) which in turn makes a request to your target URL.

In other words, your JQuery requests http://example.com/proxy/alerts.py which in turn calls that Google Alerts URL, and sends it to the JavaScript.

于 2014-01-26T23:52:25.227 回答
3

抱歉,您将无法使用跨域脚本。在这里查看答案:

jQuery XML REST 访问控制允许来源

只要 REST API 的服务器(不是您的服务器)通过设置 CORS(跨源资源共享)HTTP 标头(例如通过设置“Access-Control-Allow” -Origin”响应中的 HTTP 标头:

访问控制允许来源:*

在查看 Google 的标题时,他们没有提供该选项:

$ 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 应用程序。

于 2014-01-26T23:49:29.477 回答
0

您从SO 上的答案中复制粘贴了代码。
当然,如果不尝试从 XML 中获取确切的名称,它将无法工作。enclosure不在那个提要结果中。

一行 php 和对该 .php 文件的 AJAX 调用:

获取Feed.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>
于 2014-01-27T00:27:29.043 回答