PHP中Ajax设置多域名跨域请求

PHP中Ajax多域名跨域请求头部设置:

header('content-type:application:json;charset=utf8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');

header('Content-Type: text/html;charset=utf-8');
header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型
header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 设置允许自定义请求头的字段

一、允许所有域名访问

header('Access-Control-Allow-Origin:*); // *代表允许任何网址请求

二、允许特定单个域名访问:

header('Access-Control-Allow-Origin:https://www.xuexiareas.com/');

三、允许多个特定域名访问:

$origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';  
$allow_origin = array(
'https://www.xuexiareas.com/',
'https://six.xuexiareas.com/'
);
if(in_array($origin, $allow_origin)){
header('Access-Control-Allow-Origin:'.$origin);
}

本文通过设置Access-Control-Allow-Origin来实现跨域。  


六月初字帖坊小程序 你想要的字帖模板及工具,这里都有!