[英]Setting Access-Control-Allow-Origin header on source server
我使用$.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...
});
});
但是,由於Single Origin Policy,我收到以下錯誤:
請求的資源上不存在“Access-Control-Allow-Origin”標頭。
幸運的是,我可以訪問源服務器,因為這是我自己動態創建的RSS源。
我的問題是:如何在源服務器上設置Access-Control-Allow-Origin標頭?
編輯
我正在使用PHP,我認為我的網絡服務器是Apache。
在php中設置正確:
header('Access-Control-Allow-Origin: *');
對於apache,只需將其添加到與您嘗試遠程訪問的文件相同的目錄中的.htaccess文件中。
Header set Access-Control-Allow-Origin "*"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.