4

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.

4
  • What server-side language are you using? What webserver are you using? Commented Mar 26, 2014 at 15:19
  • I'm using PHP and I think the webserver is Apache - my hosting is with 1&1. Commented Mar 26, 2014 at 15:21
  • You have two options. Add a .htaccess file that sets the header, or have php return the file with the header. Plenty of examples here: enable-cors.org/server.html Commented Mar 26, 2014 at 15:22
  • possible duplicate of Add Access-Control-Allow-Origin to header in PHP Commented Mar 26, 2014 at 15:29

2 Answers 2

8

Set it right in the php:

header('Access-Control-Allow-Origin: *');
Sign up to request clarification or add additional context in comments.

1 Comment

This worked great for me. Is there any security advantage to having the access in the single php file over adding it to .htaccess for the site?
6

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

1 Comment

Turns out the PHP option worked for me in this instance (header("Access-Control-Allow-Origin: *");), but thanks for this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.