最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot解決ajax跨域問(wèn)題的方法

 更新時(shí)間:2018年03月06日 14:21:42   作者:幕三少  
這篇文章主要為大家詳細(xì)介紹了SpringBoot解決ajax跨域問(wèn)題的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

SpringBoot解決ajax跨域,供大家參考,具體內(nèi)容如下

一、第一種方式

1、編寫(xiě)一個(gè)支持跨域請(qǐng)求的 Configuration

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * 處理AJAX請(qǐng)求跨域的問(wèn)題
 * @author Levin
 * @time 2017-07-13
 */
@Configuration
public class CorsConfig extends WebMvcConfigurerAdapter {
  static final String ORIGINS[] = new String[] { "GET", "POST", "PUT", "DELETE" };
  @Override
  public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**").allowedOrigins("*").allowCredentials(true).allowedMethods(ORIGINS)
        .maxAge(3600);
  }
}

2、HTTP請(qǐng)求接口

@RestController
public class HelloController {

  @Autowired
  HelloService helloService;


  @GetMapping(value = "/test", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public String query() {
    return "hello";
  }
}

二、 第二種方式(推薦)

PS:第一種存在一個(gè)問(wèn)題,當(dāng)服務(wù)器拋出 500 的時(shí)候依舊存在跨域問(wèn)題

@SpringBootApplication
@ComponentScan
@EnableDiscoveryClient
public class ManagementApplication {

  public static void main(String[] args) {
    SpringApplication.run(ManagementApplication.class, args);
  }

  private CorsConfiguration buildConfig() {
    CorsConfiguration corsConfiguration = new CorsConfiguration();
    corsConfiguration.addAllowedOrigin("*");
    corsConfiguration.addAllowedHeader("*");
    corsConfiguration.addAllowedMethod("*");
    corsConfiguration.addExposedHeader(HttpHeaderConStant.X_TOTAL_COUNT);
    return corsConfiguration;
  }

  /**
   * 跨域過(guò)濾器
   *
   * @return
   */
  @Bean
  public CorsFilter corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", buildConfig()); // 4
    return new CorsFilter(source);
  }
}

2、index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>跨域請(qǐng)求</title>
<script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $.ajax({url:"http://localhost:8080/test",success:function(result){
      $("#p1").html(result);
    }});
  });
});
</script>
</head>
<body>

<p width="500px" height="100px" id="p1"></p>
<button>獲取其他內(nèi)容</button>
</body>
</html>

三、第三種方式,編寫(xiě)Filter過(guò)濾器

package com.cci.market.common.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;

/**
 * 處理跨域問(wèn)題
 * @author MR.ZHENG
 * @date 2016/08/08
 *
 */
@Component
public class OriginFilter implements Filter {

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {

  }

  @Override
  public void doFilter(ServletRequest req, ServletResponse res,
      FilterChain chain) throws IOException, ServletException {
    HttpServletResponse response = (HttpServletResponse) res;
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE,PUT");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
    chain.doFilter(req, res);
  }

  @Override
  public void destroy() {
    // TODO Auto-generated method stub

  }

}

四、Nginx跨域配置

Nginx跨域也比較簡(jiǎn)單,只需添加以下配置即可。

location / {
  proxy_pass http://localhost:8080;
  if ($request_method = 'OPTIONS') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';
    add_header 'Access-Control-Max-Age' 1728000;
    add_header 'Content-Type' 'text/plain; charset=utf-8';
    add_header 'Content-Length' 0;
    return 204;
  }
  if ($request_method = 'POST') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';
    add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';
  }
  if ($request_method = 'GET') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';
    add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';
  }
}

其中:add_header 'Access-Control-Expose-Headers' 務(wù)必加上你請(qǐng)求時(shí)所帶的header。例如本例中的“Token”,其實(shí)是前端傳給后端過(guò)來(lái)的。如果記不得也沒(méi)有關(guān)系,瀏覽器的調(diào)試器會(huì)有詳細(xì)說(shuō)明。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Jackson序列化丟失泛型的解決

    Jackson序列化丟失泛型的解決

    這篇文章主要介紹了Jackson序列化丟失泛型的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • java實(shí)現(xiàn)簡(jiǎn)單的學(xué)生管理系統(tǒng)

    java實(shí)現(xiàn)簡(jiǎn)單的學(xué)生管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單的學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Mybatis源碼解析之事務(wù)管理

    Mybatis源碼解析之事務(wù)管理

    大家好,本篇文章主要講的是Mybatis源碼解析之事務(wù)管理,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下,方便下次瀏覽
    2021-12-12
  • jpa?EntityManager?復(fù)雜查詢(xún)實(shí)例

    jpa?EntityManager?復(fù)雜查詢(xún)實(shí)例

    這篇文章主要介紹了jpa?EntityManager?復(fù)雜查詢(xún)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 關(guān)于Synchronized和ReentranLock的區(qū)別及說(shuō)明

    關(guān)于Synchronized和ReentranLock的區(qū)別及說(shuō)明

    文章介紹了Java中的`synchronized`關(guān)鍵字和`ReentrantLock`類(lèi),兩者都可以用于解決多線(xiàn)程同步問(wèn)題,但`ReentrantLock`提供了更多的功能和靈活性
    2024-12-12
  • Java Collection 移除元素方法及注意事項(xiàng)

    Java Collection 移除元素方法及注意事項(xiàng)

    這篇文章主要介紹了Java Collection 移除元素方法及注意事項(xiàng),通過(guò)一個(gè)簡(jiǎn)單實(shí)例給大家講解,需要的朋友可以參考下
    2020-01-01
  • Redis如何實(shí)現(xiàn)分布式鎖詳解

    Redis如何實(shí)現(xiàn)分布式鎖詳解

    分布式鎖一般有三種實(shí)現(xiàn)方式:1. 數(shù)據(jù)庫(kù)樂(lè)觀鎖;2. 基于Redis的分布式鎖;3. 基于ZooKeeper的分布式鎖.本篇文章將介紹第二種方式,基于Redis實(shí)現(xiàn)分布式鎖,文中有非常詳細(xì)的介紹,需要的朋友可以參考下
    2021-06-06
  • java中的構(gòu)造函數(shù)什么時(shí)候被調(diào)用執(zhí)行

    java中的構(gòu)造函數(shù)什么時(shí)候被調(diào)用執(zhí)行

    這篇文章主要介紹了java中的構(gòu)造函數(shù)什么時(shí)候被調(diào)用執(zhí)行問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • springboot解決XSS存儲(chǔ)型漏洞問(wèn)題

    springboot解決XSS存儲(chǔ)型漏洞問(wèn)題

    這篇文章主要介紹了springboot解決XSS存儲(chǔ)型漏洞問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • 關(guān)于Spring Cloud 本地屬性覆蓋的問(wèn)題

    關(guān)于Spring Cloud 本地屬性覆蓋的問(wèn)題

    這篇文章主要介紹了關(guān)于Spring Cloud 本地屬性覆蓋的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03

最新評(píng)論

溆浦县| 增城市| 新干县| 秦安县| 新田县| 资兴市| 望谟县| 嘉峪关市| 准格尔旗| 台南市| 潼关县| 社会| 台东县| 繁峙县| 五原县| 夏河县| 永济市| 门源| 甘谷县| 蓝山县| 丰镇市| 江油市| 龙南县| 密山市| 贞丰县| 铁岭市| 福贡县| 沾化县| 张北县| 东阿县| 比如县| 东乡县| 长阳| 上栗县| 星座| 云霄县| 西盟| 韶山市| 合水县| 怀来县| 辽源市|