慕工程255496
2018-11-23 14:29
@SpringBootApplication public class AjaxserverApplication { public static void main(String[] args) { SpringApplication.run(AjaxserverApplication.class, args); } @Bean public FilterRegistrationBean registerFilter() { FilterRegistrationBean bean = new FilterRegistrationBean(); bean.addUrlPatterns("/*"); bean.setFilter(new CrosFilter()); return bean; } }
public class CrosFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletResponse res = (HttpServletResponse) response; res.addHeader("Access-Control-Allow-Origin", "*"); res.addHeader("Access-Control-Allow-Methods", "*"); chain.doFilter(request, response); } @Override public void init(FilterConfig filterConfig) throws ServletException {} @Override public void destroy() {} }
但还是报错,不知道问题出在哪里,打断点也没看出问题在哪里
找到问题了,是页面上。我用的ajax请求,不是老师所写的
$.getJSON
而且没写明请求方式type:“get”,所以浏览器校验不通过。
ajax跨域完全讲解
62554 学习 · 90 问题
相似问题