直接跨域请求会出现以下错
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:*');本文转载自:精准像素:11px.cn ,感谢作者分享实用知识
