国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

springboot vue 跨域問題的解決

2024-07-14 08:42:36
字體:
來源:轉載
供稿:網友

1、Spring Boot跨域配置有兩種方法

在后端使用Spring Boot。Spring Boot跨域非常簡單,只需書寫以下代碼即可。

@Configurationpublic class CustomCORSConfiguration { private CorsConfiguration buildConfig() {  CorsConfiguration corsConfiguration = new CorsConfiguration();  corsConfiguration.addAllowedOrigin("*");  corsConfiguration.addAllowedHeader("*");  corsConfiguration.addAllowedMethod("*");   corsConfiguration.setAllowCredentials(true);   return corsConfiguration; } @Bean public CorsFilter corsFilter() {  UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();  source.registerCorsConfiguration("/**", buildConfig());  return new CorsFilter(source); }}

2.Nginx跨域配置

Spring Boot應用用Nginx反向代理。而前端跨域請求的需求不減。

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

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' 務必加上你請求時所帶的header。

例如本例中的“Token”,其實是前端傳給后端過來的。如果記不得也沒有關系,瀏覽器的調試器會有詳細說明。

三、瀏覽器設置跨域

Chrome、Firefox本身是可以通過配置支持跨域請求的。

Chrome跨域:參考文檔:Chrome跨域

四、前端Vue設置跨域

先設置 axios

axios.defaults.withCredentials = true;axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; //證明是ajax 請求psot 請求加入headers: {  'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',}

設置Config 文件下面的index.js 然后就可以再其它頁面訪問了

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西安市| 科技| 定结县| 苍山县| 巫山县| 七台河市| 运城市| 同仁县| 手游| 桦甸市| 定陶县| 临沧市| 瑞丽市| 东光县| 鄂温| 那曲县| 长丰县| 新巴尔虎左旗| 溧阳市| 四子王旗| 苗栗县| 许昌市| 尚志市| 盐津县| 大田县| 谢通门县| 寿宁县| 新野县| 东兴市| 民乐县| 开原市| 山阳县| 玛纳斯县| 龙山县| 丰宁| 上思县| 达拉特旗| 改则县| 嘉兴市| 嘉峪关市| 吉首市|