偶遇跨域怎么解决

发布时间 2023-10-30 11:33:10作者: 79524795

直接上代码

在后台写 允许跨域的域名,

$allowedDomains = array("https://example1.com", "https://example2.com");

$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';

if (in_array($origin, $allowedDomains)) {
    header("Access-Control-Allow-Origin: " . $origin);
} else {
    header("HTTP/1.1 403 Forbidden");
    die("Access denied");
}