nginx sponse to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
时间: 2023-07-17 22:09:34 浏览: 184
这个错误通常是由于缺少跨域资源共享(CORS)设置引起的。当浏览器发起跨域请求时,服务器需要返回一个带有 "Access-Control-Allow-Origin" 头的响应,以允许特定的域名访问资源。
要解决这个问题,你可以在 nginx 配置中添加以下代码来允许特定域名的跨域请求:
```
location / {
add_header Access-Control-Allow-Origin <allowed-domain>;
...
}
```
请将 `<allowed-domain>` 替换为允许访问资源的域名,或者使用通配符 `*` 来允许所有域名访问资源。如果你需要允许多个域名,可以使用逗号分隔它们。
另外,你可能还需要设置其他的 CORS 相关头,如 `Access-Control-Allow-Methods` 和 `Access-Control-Allow-Headers`,以满足你的应用程序需求。
相关问题
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
中可以通过配置解决。需要在nginx的配置文件中添加以下内容:
```
location / {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods "*";
add_header Access-Control-Allow-Headers "*";
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,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
add_header Content-Length 0;
return 204;
}
}
```
这样,就会在响应头中添加Access-Control-Allow-Origin,解决跨域问题。
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怎么配置
在 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 生效即可。
阅读全文