我创建了一个类似这样的过滤器,并在web.xml中对其进行了配置。我设法用简单的GET方法检索数据,但是当尝试发布一个新资源时,它失败了。我正在使用AngularJS,我在控制台中得到了这一点。我还没有完全理解CORS是什么,但根据我所读到的,似乎有一个“飞行前”请求(OPTIONS方法?)。我不知道我错过了什么,我怎么才能修复它?这是我得到的错误:
Origin http://localhost is not allowed by Access-Control-Allow-Origin.
代码:
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
final HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.addHeader("Access-Control-Allow-Origin", "*");
httpResponse.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
httpResponse.addHeader("Access-Control-Allow-Headers", "Authorization, X-Requested-With, Content-Type");
chain.doFilter(request, response);
}
发布于 2013-08-12 21:25:45
你必须在你的web.xml中添加一些设置。这里有一个可以帮助你http://software.dzhuvinov.com/cors-filter-installation.html的链接
https://stackoverflow.com/questions/18186221
复制相似问题