[英]CORS header 'Access-Control-Allow-Origin' missing - PHP, Angular
[英]Access-Control-Allow-Origin header Missing
我已將此JavaScript包含在其他域中:
var id = "<?php echo check_input($_GET['token']); ?>";
var querystring = "";
var lockerurl = "https:\/\/www.example.com\/contentlocker\/getlocker.php?" + 'id=' + encodeURIComponent(id) + '&r=' + encodeURIComponent(referrer) + querystring;
var iframecontents;
var old_display;
function optionstoquery(options) {
var query = "";
if (options.mt) query += "&mt=" + encodeURIComponent(options.mt);
if (options.dt) query += "&dt=" + encodeURIComponent(options.dt);
if (options.dd) query += "&dd=" + encodeURIComponent(options.dd);
if (options.md) query += "&md=" + encodeURIComponent(options.md);
return query;
}
function og_load(options) {
if (options) lockerurl += optionstoquery(options);
//preload
var xhr = new XMLHttpRequest();
xhr.open('GET', lockerurl, true);
xhr.onreadystatechange = function() {
if (this.readyState!==4) return;
if (this.status!==200) {
iframecontents = false;
return;
}
iframecontents = this.responseText;
};
xhr.send();
ogEditBody();
}
控制台顯示給我:
跨域請求被阻止:“相同來源策略”不允許在https://www.example.com/contentlocker/getlocker.php?id=3e066b64a78214a17620b5521b6d3ec4&r=aHR0cDovL2NwYWhWY1Yi51cy9yb29zdGVyMi5odG1上讀取遠程資源。 (原因:CORS標頭“ Access-Control-Allow-Origin”缺失)。
getlocker.php包含一些php和html代碼!
我使用Plesk作為網絡服務器,並嘗試通過添加getlocker.php來進行嘗試:
header('Access-Control-Allow-Origin: *');
但仍然無法正常工作!
必須在主頁請求中發送CORS標頭,因此,如果您在發送嵌入的Scrip時不夠用,則只設置它即可。
這是您的PHP文件的第一行嗎?
header("Access-Control-Allow-Origin: *");
另外,請嘗試在您的AJAX調用或HTTPRequest中發送crossDomain: true
。
在服務器端,在Linux服務器上,您可以在Web服務器配置中設置所需的標頭:
對於Plesk中的源域example.com
,請轉至Domains > example.com > Apache and nginx settings
並在Additional directives for HTTP
Additional directives for HTTPS
Additional directives for HTTP
添加以下指令:
Header set Access-Control-Allow-Origin "*"
或者,在這種情況下,可以使用具有以下內容的.htaccess
文件:
<IfModule mod_headers.c>p
Header always set Access-Control-Allow-Origin "*"
</IfModule>
如果啟用了Nginx並且需要顯示靜態內容,請在Additional nginx directives
字段中使用以下指令,而不使用Apache指令:
add_header Access-Control-Allow-Origin "*";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.