【发布时间】:2016-09-30 19:51:38
【问题描述】:
我有一个用 angular 2 制作的网页,它是一个表单,在它的最后,表单必须发送到我的 java 服务器。但是我无法发送,我收到错误Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.
我已经更改了我的服务器,所以它会发送Access-Control-Allow 标头。函数是这样的:
public static Response buildResponse(int status, Object reponseObject, MediaType mediaType) {
Response.ResponseBuilder rb = Response.status(status).entity(reponseObject);
if (mediaType != null) {
rb = rb.type(mediaType);
}
rb = rb.header("Access-Control-Allow-Origin", "*");
rb = rb.header("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE, OPTIONS");
rb = rb.header("Access-Control-Allow-Headers", "Origin, Content-Type, X-Auth-Token, accept");
rb = rb.header("Access-Control-Allow-Credentials", true);
System.out.println("teste");
return rb.build();
}
我也尝试将标题'Access-Control-Request-Method': 'POST' 添加到页面请求中,但我仍然收到错误消息。问题不在于软件功能本身,因为如果我安装并激活 chrome 的 CORS 插件,它可以正常工作,但我不能要求用户安装插件以使用我的网站。有谁知道我错过了什么?我已经搜索了一段时间,我找到的唯一解决方案是禁用此浏览器安全性(我不能要求用户这样做)并在请求中添加我已经拥有的标头。
【问题讨论】:
标签: http cors http-post httprequest