跨域问题服务端解决办法 Request header field Authorization is not allowed by Access-Control-Allow-Headers

tp5--修改全局跨域

 

在“项目目录/public/index.php“文件
在代码前面加上:

// 跨域
header("Access-Control-Allow-Origin:*");
header("Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE");
header("Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding");
 

 

tp5--修改单接口跨域

在方法里
在方法内前面加上:

// 跨域
header("Access-Control-Allow-Origin:*");
header("Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE");
header("Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding");

 

 

 

 

跨域问题服务端解决办法

一般在入口文件加

header(Access-Control-Allow-Origin:*);
// 响应类型
header(Access-Control-Allow-Methods:*);
// 响应头设置
header(Access-Control-Allow-Headers:content-type,token,id);
header("Access-Control-Request-Headers: Origin, X-Requested-With, content-Type, Accept, Authorization");

都能解决,要是不行试试下面

if($_SERVER[REQUEST_METHOD] == OPTIONS){
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
    header(Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH);
    file_put_contents(option.txt,json_encode($_REQUEST));
    exit;
}

header(Access-Control-Allow-Origin:*);
// 响应类型
header(Access-Control-Allow-Methods:*);
// 响应头设置
header(Access-Control-Allow-Headers:content-type,token,id);
header("Access-Control-Request-Headers: Origin, X-Requested-With, content-Type, Accept, Authorization");

 

跨域问题服务端解决办法 Request header field Authorization is not allowed by Access-Control-Allow-Headers

上一篇:MySQL高手进阶


下一篇:5分钟搞定 MySQL 到 TiDB 的数据同步 - CloudCanal实战