PHP跨域header('Access-Control-Allow-Origin:xxx');报错
直接跨域请求会出现以下错
XMLHttpRequest cannot load https://www.11px.cn/server.php. No 'Access-Control-Allow-Origin' header is present on the requested resource.Origin 'http://mm.11px.com' is therefore not allowed access.
PHP利用header函数解决跨域报错
1、允许单个域名访问
指定某域名(https://www.11px.cn/)跨域访问,则只需在http://m.11px.cn/server.php文件头部添加如下代码:
header('Access-Control-Allow-Origin:http://m.11px.cn/');2、允许多个域名访问
指定多个域名(https://www.11px.cn/、http://11px.cn/等)跨域访问,则只需在http://m.11px.cn/server.php文件头部添加如下代码:
$origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';
$allow_origin = array(
'http://m.11px.cn/',
'http://mm.11px.cn/'
);
if(in_array($origin, $allow_origin)){
header('Access-Control-Allow-Origin:'.$origin);
}3、允许所有域名访问
允许所有域名访问则只需在http://m.11px.cn/server.php文件头部添加如下代码:
header('Access-Control-Allow-Origin:*');资源均来自第三方,谨慎下载,前往第三方网站下载
本站所有文章、数据、图片均来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:737597453@qq.com


