激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - SpringBoot解決ajax跨域問(wèn)題的方法

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

2021-04-09 11:42幕三少 Java教程

這篇文章主要為大家詳細(xì)介紹了SpringBoot解決ajax跨域問(wèn)題的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

一、第一種方式

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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)求接口

?
1
2
3
4
5
6
7
8
9
10
11
12
@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)題

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!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ò)濾器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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)單,只需添加以下配置即可。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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í)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://www.cnblogs.com/smiler/p/8509062.html

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25
主站蜘蛛池模板: 成人免费一区二区三区视频网站 | 午夜精品福利视频 | 黄色电影免费网址 | 成人福利视频在 | 天堂在线资源av | 女女久久 | 欧美一级aa免费毛片 | 免费观看黄视频 | 免费黄色小视频网站 | 欧美雌雄另类xxxxx | 久久华人 | 自拍亚洲伦理 | 欧美日本色 | 国产在线观看av | 91精品国产91 | 欧美高清一级片 | 久久tv免费国产高清 | 成人毛片视频免费看 | 亚洲一区二区三区视频免费 | 99精品无人区乱码在线观看 | 国产精品亚洲一区二区三区在线观看 | 国产激爽大片在线播放 | 欧美一区二区三区成人精品 | 国产伦精品一区二区三区 | 国产精品视频二区不卡 | 欧美成人午夜一区二区三区 | 欧美片一区二区 | 88xx成人永久免费观看 | 精品视频一区二区三区四区 | 高潮激情aaaaa免费看 | 亚洲网站在线 | 免费观看一区二区三区视频 | 91高清免费在线观看 | 在线看免费观看av | 欧美城天堂网 | 国产99久久久久久免费看农村 | 最新中文字幕在线 | 久久99国产精品二区护士 | 精品麻豆cm视频在线看 | 久久新网址 | 蜜桃免费在线 |