在源服务器上设置Access-Control-Allow-Origin标头

Seb*_*ian 4 php apache rss jquery cors

$.get用来解析jQuery中的RSS提要,代码类似于:

$.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...
    });
});
Run Code Online (Sandbox Code Playgroud)

但是,由于Single Origin Policy,我收到以下错误:

请求的资源上不存在"Access-Control-Allow-Origin"标头.

幸运的是,我可以访问源服务器,因为这是我自己动态创建的RSS源.

我的问题是:如何在源服务器上设置Access-Control-Allow-Origin标头?

编辑

我正在使用PHP,我认为我的网络服务器是Apache.

ino*_*nik 8

在php中设置正确:

header('Access-Control-Allow-Origin: *');
Run Code Online (Sandbox Code Playgroud)


use*_*654 6

对于apache,只需将其添加到与您尝试远程访问的文件相同的目录中的.htaccess文件中.

Header set Access-Control-Allow-Origin "*"
Run Code Online (Sandbox Code Playgroud)

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