在项目入口文件index.php 中添加如下代码:
header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型
header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
header('Access-Control-Allow-Headers: Accept,Content-Type,Content-Length,Accept-Encoding,X-Requested-with,Origin,X-Custom-Header,token,group-id,LANG,Accept-Serial,accept-serial,accept-platform,lng,lat,user-type,env');
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
// header('Access-Control-Allow-Headers:x-requested-with,Content-type');
header('Access-Control-Allow-Headers: Accept,Content-Type,Content-Length,Accept-Encoding,X-Requested-with,Origin,X-Custom-Header,user-token,group-id,LANG,Accept-Serial,accept-serial,accept-platform,lng,lat,user-type,env');
//浏览器页面ajax跨域请求会请求2次,第一次会发送OPTIONS预请求,不进行处理,直接exit返回,但因为下次发送真正的请求头部有带token,所以这里设置允许下次请求头带token否者下次请求无法成功
exit("ok");
}