Docs/linux基础/nginx/ajax跨域.md
2022-10-18 16:59:37 +08:00

17 lines
No EOL
1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

```
if ($request_method = 'OPTIONS') {
# 表示允许这个域跨域调用(客户端发送请求的域名和端口)
# $http_origin动态获取请求客户端请求的域 不用*的原因是带cookie的请求不支持*号
add_header Access-Control-Allow-Origin $http_origin;
# 表示请求头的字段 动态获取
add_header Access-Control-Allow-Headers $http_access_control_request_headers;
# 该字段可选。它的值是一个布尔值表示是否允许发送Cookie。默认情况下
# Cookie不包括在CORS请求之中。设为true即表示服务器明确许可
# Cookie可以包含在请求中一起发给服务器。这个值也只能设为true
# 如果服务器不要浏览器发送Cookie删除该字段即可
add_header Access-Control-Allow-Credentials true;
# 指定允许跨域的方法,*代表所有
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
return 200;
}
```