Access to XMLHttpRequest at 'https://kf.thunisoft.com/webcall/ai/selfHelp.do' from origin 'https://kf.thunisoft.com:5000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 出现这个问题,在nginx怎么配置
时间: 2024-04-07 19:28:13 浏览: 15
在 nginx 中解决跨域问题可以通过添加响应头的方式。在 nginx 的配置文件中添加以下内容:
```
location / {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
add_header Content-Length 0;
return 204;
}
}
```
上述配置中,`add_header Access-Control-Allow-Origin *;` 表示允许任意来源的数据请求;`add_header Access-Control-Allow-Credentials true;` 表示允许发送 Cookie;`add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';` 表示允许的请求方法;`add_header Access-Control-Allow-Headers 'DNT,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';` 表示允许的请求头。
配置完成后,重启 nginx 生效即可。