问答详情
源自:3-11 用户模型管理--用户注册功能实现02

AJAX跨域问题

可以不可以后端不做跨域的任何处理,全部让前端来做,比如Nginx?

提问者:321121254 2018-12-08 01:33

个回答

  • 慕侠3183483
    2018-12-09 10:42:14

    可以前端做,但是一般都是后端做,写一个统一过滤器也可以

     @Override

        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

            HttpServletResponse response = (HttpServletResponse) res;

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

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

            response.setHeader("Access-Control-Max-Age", "3600");

            response.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

            chain.doFilter(req, res);

        }

  • yangxuefeng
    2018-12-09 04:13:37

    可以用 openresty