跨域Response to preflight request doesn‘t pass access control check: It does not have HTTP ok status.

最近后端添加自定义请求头拦截器后,前端也增加请求头后,解决过程中先后出现两种跨域错误。
一、请求后台出现如下跨域问题:
has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: It does not have HTTP ok status.
跨域Response to preflight request doesn‘t pass access control check: It does not have HTTP ok status._第1张图片
此问题是前端请求后台时先发送options预检请求(浏览器机制自动发送不带传参),由于设置自定义请求头拦截器,所以当options请求过来时,返回500.因为需要对options这种后台特殊响应。
参考链接见:http://www.mamicode.com/info-detail-2790732.html,中Access-Control-Allow-Methods的值。
如果不止一个拦截器,也需处理,目的都是为了对options预检不校验直接放行。
二、Request header field XXX is not allowed by Access-Control-Allow-Headers in preflight response.
跨域Response to preflight request doesn‘t pass access control check: It does not have HTTP ok status._第2张图片

此问题是对header中自定义请求头,需要加上放行;如何加,同样可参考链接:http://www.mamicode.com/info-detail-2790732.html。
对字段Access-Control-Allow-Headers的value值增加XXX(特别注意,填是解决不了的,因为 里面不包含个性化自定义的头参数)

response.setHeader("Access-Control-Allow-Headers", "content-type,XXX");
response.setHeader("Access-Control-Allow-Methods", "*");
response.setHeader("Access-Control-Allow-Origin", origin);
response.setHeader("Access-Control-Allow-Credentials", "true");

此文章仅供参考,如有错误,接受指正~

你可能感兴趣的:(java)