package com.szx.util; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import com.szx.service.SzxService; /** * 拦截器(注:)解决跨域问题 还需要在配置文件中配置 * * @author Strvie * */ @Component public class CORSInterceptor implements HandlerInterceptor { private static final Logger log = LogManager.getLogger(CORSInterceptor.class.getName()); public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { } public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { } public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception { response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Headers", "x-requested-with"); return true; } }
上边代码是 拦截器类
2 在配置文件中 添加配置
<!-- 跨域访问 -->
<mvc:interceptors>
<bean class="com.szx.util.CORSInterceptor" />
</mvc:interceptors>
这样H5 写app中请求ajax访问就解决了跨域问题