浏览器请求分为简单请求和复杂请求
在发起复杂请求且跨域的情况下会自动发起OPTIONS预检测请求

简单请求需满足以下两个条件否则为复杂请求
请求方法是以下三种方法之一:
HEAD、GET、POST
HTTP 的头信息不超出以下几种字段:
Accept、Accept-Language、Content-Language、Last-Event-ID、
Content-Type: 只限于 (application/x-www-form-urlencoded、multipart/form-data、text/plain)

#nginx 配置文件 server
add_header 'Access-Control-Allow-Origin' '*'; #允许跨域
add_header 'Access-Control-Allow-Credentials' "true"; #是否携带cookie
add_header 'Access-Control-Allow-Headers' 'X-Requested-With,token';#允许的header名称
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';#允许的请求方法
add_header 'Access-Control-Max-Age' 86400;#预检测请求的缓存时间另外浏览器控制面板的Disable cache不勾选才可生效
#nginx 伪静态 location
if ($request_method = 'OPTIONS') {#拦截预检测请求
   return 200; 
 }
Logo

前往低代码交流专区

更多推荐