1. 程式人生 > >Filter解決ajax跨域問題

Filter解決ajax跨域問題

-a json ces all -m ons main charset -h

public class CrossDomainFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            
throws IOException, ServletException { HttpServletResponse resp = (HttpServletResponse) response; // 允許跨域ajax請求 resp.setHeader("Access-Control-Allow-Origin", "*"); resp.setHeader("Access-Control-Allow-Methods", "*"); resp.setHeader("Access-Control-Allow-Headers", "accept, origin, content-type"); resp.setHeader(
"Content-Type", "application/json;charset=UTF-8"); chain.doFilter(request, resp); } @Override public void destroy() { // TODO Auto-generated method stub } }

Filter解決ajax跨域問題