Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Access-Control-Allow-Origin header on source server

I am using $.get to parse an RSS feed in jQuery with code similar to this:

$.get(rssurl, function(data) {
    var $xml = $(data);
    $xml.find("item").each(function() {
        var $this = $(this),
            item = {
                title: $this.find("title").text(),
                link: $this.find("link").text(),
                description: $this.find("description").text(),
                pubDate: $this.find("pubDate").text(),
                author: $this.find("author").text()
        }
        //Do something with item here...
    });
});

However, due to the Single Origin Policy, I'm getting the following error:

No 'Access-Control-Allow-Origin' header is present on the requested resource.

Fortunately I have access to the source server, as this is my own dynamically created RSS feed.

My question is: how do I set the Access-Control-Allow-Origin header on my source server?

Edit

I'm using PHP and I think my webserver is Apache.

like image 864
Sebastian Avatar asked Dec 09 '22 08:12

Sebastian


2 Answers

Set it right in the php:

header('Access-Control-Allow-Origin: *');
like image 183
inorganik Avatar answered Dec 11 '22 10:12

inorganik


For apache, you simply add this to a .htaccess file in the same directory as the file you are trying to access remotely.

Header set Access-Control-Allow-Origin "*"

http://enable-cors.org/server_apache.html

like image 31
Kevin B Avatar answered Dec 11 '22 11:12

Kevin B