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

14,945

Solution 1

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

Solution 2

Set it right in the php:

header('Access-Control-Allow-Origin: *');
Share:
14,945
Sebastian
Author by

Sebastian

Updated on June 04, 2022

Comments

  • Sebastian
    Sebastian almost 2 years

    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.

  • Sebastian
    Sebastian about 10 years
    Turns out the PHP option worked for me in this instance (header("Access-Control-Allow-Origin: *");), but thanks for this answer.
  • Tim Erickson
    Tim Erickson over 8 years
    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?