【发布时间】:2015-06-12 16:28:38
【问题描述】:
我已经使用 Spring REST 公开了一个 API,并尝试从独立的 jquery 代码中发布数据。这是一个跨域请求。
我的配置如下
春天 - 3.2.5.RELEASE , jquery - 1.10.2
$.ajax({
type:"POST",
beforeSend: function (request)
{
request.setRequestHeader("Content-Type","application/json");
request.setRequestHeader("Authorization", basic);
request.setRequestHeader("Access-Control-Allow-Origin","*");
},
url: "http://localhost:8080/workflow/workflow-api/human",
data: '{"id":"1","firstName":"Tito","lastName":"LastName"}',
crossDomain:true,
processData: false,
success: function(msg) {
console.log( "Login result:"+msg);
}
});
在 Chrome 开发者控制台中出现以下错误
XMLHttpRequest cannot load http://localhost:8080/stockingworkflow/workflow-api/human. Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers.
在 web.xml 中,CORS 过滤器条目如下
<filter>
<filter-name>cors</filter-name>
<filter-class>com.workflow.security.CorsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>cors</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我的过滤器类如下
@Component
public class CorsFilter extends OncePerRequestFilter {
private static Logger logger = Logger.getLogger(AuthenticationService.class);
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
logger.debug("<-----Inside CORS filter---->");
if (request.getMethod().equals("OPTIONS")) {
logger.debug("<-----Inside OPTIONS---->");
response.addHeader("Access-Control-Allow-Origin","*");
response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
response.addHeader("Access-Control-Allow-Headers", "x-requested-with");
response.addHeader("Access-Control-Max-Age", "1800");
}
filterChain.doFilter(request, response);
}
}
我通过在浏览器中打开一个 html 文件来调用 jquery ajax 请求。该 html 文件不是由服务器提供的。所以我在浏览器中的 URL 是
file:///C:/Users/3467/Desktop/workflow/index.html
由于我启用了跨域请求的过滤器,理想情况下 jquery ajax 请求应该可以工作。但它说:
请求头域 Access-Control-Allow-Origin 不允许 访问控制允许标头
在“网络”下的开发人员控制台中,我看到 OPTIONS GET 请求是由浏览器发送的,它返回 200 OK。意味着它命中了 CORS 过滤器。下面是来自 Chrome 开发者工具的请求和响应标头。
Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/workflow/workflow-api/human
Request Method:OPTIONS
Status Code:200 OK
Request Headers
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,hi;q=0.6,id;q=0.4,ms;q=0.2
Access-Control-Request-Headers:access-control-allow-origin, accept, authorization, content-type
Access-Control-Request-Method:POST
Cache-Control:max-age=0
Connection:keep-alive
DNT:1
Host:localhost:8080
Origin:null
User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.66 Safari/537.36
Response Headers
Access-Control-Allow-Headers:x-requested-with
Access-Control-Allow-Methods:GET, POST, PUT, DELETE
Access-Control-Allow-Origin:*
Access-Control-Max-Age:1800
Allow:GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
Content-Length:0
Date:Mon, 22 Sep 2014 21:54:29 GMT
Server:Apache-Coyote/1.1
我不知道出了什么问题,我关注了许多关于 CORS 的 SO 和 Spring REST 博客。不知道我是否遗漏了什么。
【问题讨论】:
标签: jquery spring spring-mvc cors