天天看点

ajax 跨域错误过滤器,AJAX跨域访问如何再服务器中添加过滤器

packagecom.TestFilter;importjava.io.IOException;importjavax.servlet.Filter;importjavax.servlet.FilterChain;importjavax.servlet.FilterConfig;importjavax.servlet.ServletException;importjavax.servlet.ServletRequest;importjavax.servlet.ServletResponse;importjavax.servlet.annotation.WebFilter;importjavax.servlet.http.HttpServletResponse;@WebFilter("/TestFilter")public class TestFilter implementsFilter {

publicTestFilter() {//TODO Auto-generated constructor stub

}

public voiddestroy() {//TODO Auto-generated method stub

}

public void doFilter(ServletRequest request, ServletResponse servletResponse, FilterChain chain) throwsIOException, ServletException {

HttpServletResponse response= (HttpServletResponse)servletResponse;

response.setHeader("Access-Control-Allow-Origin","*");

response.setHeader("Access-Control-Allow-Methods","GET,POST,PUT,OPTIONS");

response.setHeader("Access-Control-Allow-Credentials","true");

chain.doFilter(request, response);

System.out.println(" **********************CROS 执行了 **********************");

}

public void init(FilterConfig fConfig) throwsServletException {//TODO Auto-generated method stub

}

}