JS跨域请求外部服务器的资源
tomcat服务器提供的接口,不能在其他域中访问的时候,需要增Access-Control-Allow-Origin:*
以下操作均在需要被访问的项目中设置
1、编写拦截器代码
packagecom.soft.filter; /** *CreatedbyAdministratoron2017/2/6. */ importjavax.servlet.*; importjavax.servlet.http.HttpServletResponse; importjava.io.IOException; publicclassCORSFilterimplementsFilter{ @Override publicvoidinit(FilterConfigfilterConfig)throwsServletException{ } @Override publicvoiddoFilter(ServletRequestservletRequest,ServletResponseservletResponse,FilterChainfilterChain)throwsIOException,ServletException{ HttpServletResponsehttpResponse=(HttpServletResponse)servletResponse; httpResponse.addHeader("Access-Control-Allow-Origin","*"); filterChain.doFilter(servletRequest,servletResponse); } @Override publicvoiddestroy(){ } }
2、配置web.xml
<filter> <filter-name>CorsFilter</filter-name> <filter-class>com.omiaozu.manager.filter.CORSFilter</filter-class> </filter> <filter-mapping> <filter-name>CorsFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>