nginx的需要增加的配置
location /h5 {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
if ($request_method = 'OPTIONS') {
return 204;
}
}
apache需要增加的配置
# 启用headers模块相关配置
<IfModule headers_module>
# 设置允许跨域的源,这里设置为允许所有域,若要指定具体域,替换*为具体域名,如 https://example.com
Header set Access-Control-Allow-Origin "*"
# 设置允许的跨域请求方法
Header set Access-Control-Allow-Methods "GET, POST, OPTIONS"
# 设置预检请求(OPTIONS)缓存时间(单位:秒)
Header set Access-Control-Max-Age 1728000
# 设置允许的请求头信息
Header set Access-Control-Allow-Headers "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range"
</IfModule>
# 启用rewrite模块来处理OPTIONS请求并返回204状态码
<IfModule rewrite_module>
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ - [R=204,L]
</IfModule>