parsing google alert feed

1.9k Views Asked by At

I try to parse a Google Alert feed via ajax but I get exception. This is what I tried:

$(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 />");
        });
    });
});

But I get:

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. 

Any help?


P.S: I already read several posts about parsing rss feeds via ajax and jquery, but none of them worked for me.

3

There are 3 best solutions below

0
On

Sorry, you won't be allowed to use cross-domain scripting. See the answers here:

jQuery XML REST Access-Control-Allow-Origin

It is not possible as long as the server of the REST API (not your server) allows the request from a different origin by setting the CORS (Cross-Origin Resource Sharing) HTTP header, for example by setting the "Access-Control-Allow-Origin" HTTP header in the response:

Access-Control-Allow-Origin: *

When looking at Google's headers, they don't provide that option:

$ 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

Your only alternative is to use your server to fetch the values, then relay the values to your jQuery app.

0
On

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.

0
On

You plain copy-pasted the code from an answer here on SO.
Sure it will not work without at least trying to get the exact names out of that XML. enclosure is not in that feed results.

One line of php and an AJAX call to that .php file:

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>

proof of concept? this the result I get:

<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>