直接跨域请求会出现以下弄错
XMLHttpRequest cannot load https://www.11dz.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.11dz.cn/)跨域出访,则只需在http://m.11dz.cn/server.php文件头部添加如下代码:
header('Access-Control-Allow-Origin:http://m.11dz.cn/');
2、允许多个域名出访
选定多个域名(https://www.11dz.cn/、http://11dz.cn/等)跨域访问,则只需在http://m.11dz.cn/server.php文件头部添加如下代码:
$origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : ''; $allow_origin = array( 'http://m.11dz.cn/', 'http://mm.11dz.cn/' ); if(in_array($origin, $allow_origin)){ header('Access-Control-Allow-Origin:'.$origin); }
3、允许所有域名出访
容许所有域名访问则只需在http://m.11dz.cn/server.php文件头部添加如下代码:
header('Access-Control-Allow-Origin:*');