跨域设置

  1. 设置允许跨域的域名
// 允许所有域名访问
header('Access-Control-Allow-Origin:*');
// 允许指定域名访问
header('Access-Control-Allow-Origin:http://localhost:4000');
// 允许多个域名访问
$origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';
$allow_origin = array(
  'http://localhost:3000',
  'http://localhost:4000'
);
if(in_array($origin, $allow_origin)){
  header('Access-Control-Allow-Origin:'.$origin);
}
  1. 设置允许的响应类型
// 设置单个相应类型
header('Access-Control-Allow-Methods:POST');
// 设置多个响应类型
header('Access-Control-Allow-Methods:POST,GET,OPTIONS');
  1. 设置允许的响应头
header('Access-Control-Allow-Headers:x-requested-with,content-type');

参考资料

MIT Licensed | Copyright © 2018-present 滇ICP备16006294号

Design by Quanzaiyu | Power by VuePress