【发布时间】:2021-06-25 11:24:37
【问题描述】:
我遇到了这样的错误。
阻止对另一个来源的资源的请求:“同源策略”不允许加载远程资源 from https://test-domain.com/public/fetch_data.php/(缺少 CORS 标头“Access-Control-Allow-Origin”)。
我在配置文件的开头添加了代码https://test-domain.com/private/initialize.php
$accepted_origins = array("https://test-domain.com");
if(isset($_SERVER['HTTP_ORIGIN'])){
if(in_array($_SERVER['HTTP_ORIGIN'], $accepted_origins)){
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
}else{
header("HTTP/1.1 403 Origin Denied");
return;
}
}
我的问题:够用还是有什么要补充的?
【问题讨论】:
-
My question: Is it enough or is there anything else to add?你已经用那个代码测试了吗? -
@Baracuda078 是 -> 如果 ip 用于测试
1.1.1.1有错误HTTP/1.1 403 Origin Denied(console->network) -
那么
$_SERVER['HTTP_ORIGIN']包含什么?回显它或使用 var_dump() -
@Baracuda078 我找到了解决方案
https://stackoverflow.com/questions/41975920/how-to-send-a-cross-domain-ajax-request