PHP 跨域处理
跨域访问失败是会出现
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
报错

处理方法:


1.返回的头信息中增加

header("Access-Control-Allow-Origin: *");

2.允许多个地址跨域

$origin = isset($_SERVER['HTTP_ORIGIN'])?$_SERVER['HTTP_ORIGIN']:'';    
$allow_origin = [
        'http://page.test.cn',    
        'http://page.test.cn',
    ];
    if (in_array($origin, $allow_origin)) {
        header('Access-Control-Allow-Origin:'.$origin);
        header("Access-Control-Allow-Credentials: true");
    }

这里有个规则,当 header("Access-Control-Allow-Credentials: true");当允许跨域携带cookie时,不允许header("Access-Control-Allow-Origin: *"); 设置为'*';