接口请求跨域Access-Control-Allow-Origin
1、修改nginx配置,添加下面内容:
location / { # 设置允许跨域的来源域名列表 add_header 'Access-Control-Allow-Origin' "*" always; # 设置允许跨域的请求方法 add_header 'Access-Control-Allow-Methods' '*'; # 设置允许携带 Cookie add_header 'Access-Control-Allow-Credentials' 'true'; # 设置处理预检请求的时长(OPTIONS 请求) add_header 'Access-Control-Max-Age' 3600; # 设置允许请求携带的请求头 add_header 'Access-Control-Allow-Headers' '*'; # 这里根据实际情况进行配置,如允许跨域访问的子路径等 # ... }2、修改代码入口文件,添加一下代码
header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods:POST'); header('Access-Control-Allow-Headers:x-requested-with,content-type');