-3

I came across a problem using jQuery to retrieve an RSS feed located on an external domain. It was working in Safari but other browsers would error because of Same Origin Policy restrictions (which are also documented about the $.ajax() function).

Wanna know how I fixed it?

Matt Scheurich
  • 845
  • 2
  • 10
  • 23
  • Uh, well, I think the usual solution is to provide a proxy script that IS on the same domain as your page. That, or JSONP. – tdammers Jan 13 '11 at 23:32

2 Answers2

3

There are three ways to get around the Same-Origin Policy:

  1. Proxy -- as Strawberry Sheurbert did, perfectly effective but a waste of bandwidth and computing power
  2. JSONP -- loading the data through the script tag. Needs cooperation from source website and basically hackish and clumsy.
  3. CORS -- the "right" way, elegant and nuanced, but needs a lot of cooperation from source website and doesn't work with older browsers.

You pays your money and you takes your chance.

Malvolio
  • 38,966
  • 24
  • 87
  • 125
-2

I made a simple PHP script like so:

<?php

/*
    fetch.php fixes this issue: http://en.wikipedia.org/wiki/Same_origin_policy

    Read more:
        *   http://api.jquery.com/jQuery.ajax/
        *   http://stackoverflow.com/questions/3595515/xmlhttprequest-error-origin-null-is-not-allowed-by-access-control-allow-origin
        *   http://stackoverflow.com/questions/1653308/access-control-allow-origin-multiple-origin-domains
*/

// Requires URL
if ( !isset($_REQUEST['url']) || empty($_REQUEST['url']) ) exit( 'No url specified' );

// Set content-type
$type = 'application/rss+xml; charset=utf-8;';
if ( isset($_REQUEST['type']) && !empty($_REQUEST['type']) ) {
    $type = urldecode($_REQUEST['type']);
}

// Adapted from http://www.howtogeek.com/howto/programming/php-get-the-contents-of-a-web-page-rss-feed-or-xml-file-into-a-string-variable/
function get_url_contents( $url ){
    if ( function_exists('curl_init') ) {
        $crl = curl_init();
        $timeout = 5;
        curl_setopt ($crl, CURLOPT_URL, $url);
        curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
        $ret = curl_exec($crl);
        curl_close($crl);
        return $ret;
    } else {
        return file_get_contents( $url );
    }
    return 'Could not retrieve url';
}

// Output content from url
header( 'Content-type: ' . $type );
echo get_url_contents( urldecode($_REQUEST['url']) );


?>

It's pretty rubbish looking, but it works well enough for the moment. I hope it helps.

Matt Scheurich
  • 845
  • 2
  • 10
  • 23
  • 2
    Also, why are you answering your own question? – tdammers Jan 13 '11 at 23:32
  • To get double points: as a question maker and an answer poster :) – dzendras Jan 13 '11 at 23:34
  • I had a problem, then found a solution. I'm not saying it's the best out of all possible solutions in the world, but it was one that worked for my application (most solutions relied on another site's co-operation or were for another application, i.e. loading JSON data). Maybe someone finds it useful as a solution or as a springboard for alternative solutions. – Matt Scheurich Jan 14 '11 at 15:21