2021/07/28

php解决跨域问题


以下是这边php后端常用解决跨域的配置。

// 允许所有域名访问
header('Access-Control-Allow-Origin: *');
// 允许请求
header('Access-Control-Allow-Headers: x-requested-with,content-type');
// 允许请求方式
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, OPTIONS');
// 允许是否携带cookie凭证,如果是true这后端可收到cooki信息
header('Access-Control-Allow-Credentials: false');

最近一位老同事在跨域请求时出现了 no-referrer-when-downgrade 错误。原因是前端如果配置了 xhrFields.withCredentials = true 时,必须在后端 头信息 Access-Control-Allow-Origin 指定域名,而不能指定为 *