前端跨域访问了,nginx怎么配置

nginx配置跨域访问后 get,post可以访问,但是put方法不行

具体配置如下

add_header Access-Control-Allow-Origin *;

location / {
   if ($request_method = 'OPTIONS') {
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Headers X-Requested-With;
    add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
    return 204;
}

然后报错提示了

index.html:1 Failed to load http://ms.iot-sw.net:7880/service/v1/shareport/park/4: Method PUT is not allowed by Access-Control-Allow-Methods in preflight response.

有哪位大神知道怎么配置可以支持put方法吗

阅读 13k
1 个回答

这种配置只处理了 OPTIONS 请求。OPTIONS 请求不转给后端,直接返回 204,并允许跨域。

对于其它请求,需要在前面再加一段:

location / {
   add_header Access-Control-Allow-Origin *;
   add_header Access-Control-Allow-Headers X-Requested-With;
   add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
    
   if ($request_method = 'OPTIONS') {
     return 204;
   }
}
推荐问题