JS如何實(shí)現(xiàn)跨域請(qǐng)求外部服務(wù)器的資源

小編給大家分享一下JS如何實(shí)現(xiàn)跨域請(qǐng)求外部服務(wù)器的資源,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

成都創(chuàng)新互聯(lián)公司專注于右江企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站建設(shè)。右江網(wǎng)站建設(shè)公司,為右江等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站開發(fā),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)

tomcat服務(wù)器提供的接口,不能在其他域中訪問的時(shí)候,需要增 Access-Control-Allow-Origin:*

以下操作均在需要被訪問的項(xiàng)目中設(shè)置

1、編寫攔截器代碼

 package com.soft.filter;
/**
 * Created by Administrator on 2017/2/6.
 */
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class CORSFilter implements Filter {
  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
  }
  @Override
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
    httpResponse.addHeader("Access-Control-Allow-Origin", "*");
    filterChain.doFilter(servletRequest, servletResponse);
  }
  @Override
  public void destroy() {
  }
}

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>

以上是“JS如何實(shí)現(xiàn)跨域請(qǐng)求外部服務(wù)器的資源”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

當(dāng)前題目:JS如何實(shí)現(xiàn)跨域請(qǐng)求外部服務(wù)器的資源
網(wǎng)頁網(wǎng)址:http://muchs.cn/article6/pihiog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、標(biāo)簽優(yōu)化、品牌網(wǎng)站設(shè)計(jì)、定制網(wǎng)站、微信公眾號(hào)、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)