Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response
预运行响应中的访问控制允许头不允许请求头字段内容类型,也就是我们的请求head中包含了不允许的字段内容。
原因:
包含自定义header字段的跨域请求,浏览器会先向服务器发送OPTIONS请求,探测该服务器是否允许自定义的跨域字段。如果允许,则继续实际的POST/GET正常请求,否则,返回标题所示错误。
解决方案:
需要在系统的过滤器(filter)中设置响应头包含该字段
HttpServletResponse rep = (HttpServletResponse) servletResponse;
rep.setHeader("Access-Control-Allow-Origin", "*");
rep.setHeader("Access-Control-Allow-Credentials", "true");
// 允许的访问方法
rep.setHeader("Access-Control-Allow-Methods","POST, GET, PUT, OPTIONS, DELETE, PATCH");
// Access-Control-Max-Age 用于 CORS 相关配置的缓存
rep.setHeader("Access-Control-Max-Age", "3600");
rep.setHeader("Access-Control-Allow-Headers","Access-Control-Allow-Headers, content-type,x-requested-with,Authorization, x-ui-request,lang");