您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)如何在Springmvc中利用ajax實現(xiàn)一個跨域請求,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
寫一個類 繼承HandlerInterceptorAdapter
package com.util; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; public class CommonInterceptor extends HandlerInterceptorAdapter { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "*"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); response.setHeader("Access-Control-Allow-Credentials", "true"); // 是否允許瀏覽器攜帶用戶身份信息(cookie) return true; } }
然后在xml里面配置一下路徑
<mvc:interceptors> <!--過濾所有請求,處理跨域請求問題 --> <mvc:interceptor> <mvc:mapping path="/**" /> <bean class="com.util.CommonInterceptor" /> </mvc:interceptor> </mvc:interceptors>
這樣就可以了 不過有篇博客說針對簡單跨域沒問題。但是針對post+json請求卻失敗,提示跨域失敗。所以下面記錄一下他的解決方式
建立一個類 繼承 OncePerRequestFilter
public class CrossFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) { // CORS "pre-flight" request response.addHeader("Access-Control-Allow-Origin", "*"); response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); response.addHeader("Access-Control-Allow-Headers", "Content-Type"); response.addHeader("Access-Control-Max-Age", "1800");//30 min } filterChain.doFilter(request, response); } }
web.xml里面設(shè)置一下
<filter> <filter-name>cors</filter-name> <filter-class>cn.***.filter.CrossFilter</filter-class> </filter> <filter-mapping> <filter-name>cors</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
然后就可以了 上述方式為spring3.0版本 如果是4.0版本可以用下面方式(無測)
xml里面配置
<mvc:cors> <mvc:mapping path="/**" allowed-origins="*" allow-credentials="true" max-age="1800" allowed-methods="GET,POST,OPTIONS"/> </mvc:cors>
PS:使用SpringMVC解決Ajax跨域問題
package com.mengyao.system.filter; import java.io.IOException; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.filter.OncePerRequestFilter; /** * CORSFilter 解決跨域問題 * @author mengyao * */ @Component public class InitCORSFilter extends OncePerRequestFilter { private Logger logger = LoggerFactory.getLogger(InitCORSFilter.class); public InitCORSFilter() { logger.info("==== 初始化系統(tǒng)允許跨域請求 ===="); } /** * 解決跨域:Access-Control-Allow-Origin,值為*表示服務(wù)器端允許任意Domain訪問請求 */ @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) { response.addHeader("Access-Control-Allow-Origin", "*"); response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); response.addHeader("Access-Control-Allow-Headers", "origin, content-type, accept, x-requested-with, sid, mycustom, smuser"); response.addHeader("Access-Control-Max-Age", "1800");//30 min } filterChain.doFilter(request, response); } }
看完上述內(nèi)容,你們對如何在Springmvc中利用ajax實現(xiàn)一個跨域請求有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。