vue前后端端口不一致的問題解決
在config index.js文件中

引入如下代碼即可
const path = require('path')
const devEnv = require('./dev.env')
module.exports = {
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: devEnv.OPEN_PROXY === false ? {} : {
'/api': {
target: 'http://localhost:8083',
changeOrigin: true,
pathRewrite: {
'^/api': '/'
}
}
},
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: devEnv.OPEN_PROXY === false ? {} : {
'/api': {
target: 'http://localhost:8083', //需要更改的后端的端口號
changeOrigin: true,
pathRewrite: {
'^/api': '/'
}
}
},
這里的配置是正則表達式,以/api開頭的將會被用用‘/api'替換掉,假如后臺文檔的接口是 /api/list/xxx
//前端api接口寫:axios.get('/api/list/xxx') , 被處理之后實際訪問的是:http://news.baidu.com/api/list/xxx
}
}},解決前后端不同端口號的跨域請求問題
擬定前端端口號8000;后端端口號是8070
前端使用的Vue框架,默認數(shù)據(jù)信息存儲在 .eve.development中,需要配置(修改)前端數(shù)據(jù)發(fā)送的路徑
NODE_ENV=development //設置VUE_APP_PREVIEW=false , VUE_APP_PREVIEW=false //URL后接后端tomcat服務地址http://localhost:8070/ VUE_APP_API_BASE_URL=http://localhost:8070/
方式1,在后端啟動文件同級目錄,創(chuàng)建一個目錄Corsconfig
package com.mashibing.config;
//當通過文件配置跨域請求時,則配置的是全局
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@Configuration
public class Corsconfig {
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
// 設置屬性
// 允許跨域請求的地址,*表示所有
corsConfiguration.addAllowedOrigin("*");
// 跨域的請求頭
corsConfiguration.addAllowedHeader("*");
// 跨域的請求方法
corsConfiguration.addAllowedMethod("*");
// 在跨域請求的時候使用同一個 Session
corsConfiguration.setAllowCredentials(true);
return corsConfiguration;
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//配置 可以訪問的地址
source.registerCorsConfiguration("/**", buildConfig());
return new CorsFilter(source);
}
}
方式2,通過注解的方式,在需要和前端交互數(shù)據(jù)的頁面配置注解@CrossOrigin
package com.mashibing.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@CrossOrigin(origins = "*",methods = {},allowedHeaders = "*",allowCredentials = "true")
public class test {
@RequestMapping("/auth/login")
public String test1(){
System.out.println("Success");
return "";
}
}到此這篇關于vue前后端端口不一致的問題解決 的文章就介紹到這了,更多相關vue前后端端口不一致內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue 2.0 中依賴注入 provide/inject組合實戰(zhàn)
這篇文章主要介紹了Vue 2.0 依賴注入 provide/inject組合,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-06-06
vue中使用element ui的input框?qū)崿F(xiàn)模糊搜索的輸入框
這篇文章主要介紹了vue中使用element ui的input框?qū)崿F(xiàn)模糊搜索的輸入框,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11
vue3中使用v-model實現(xiàn)父子組件數(shù)據(jù)同步的三種方案
這篇文章主要介紹了vue3中使用v-model實現(xiàn)父子組件數(shù)據(jù)同步的三種方案,如果只有一個匿名v-model的傳遞的話,可以使用vue3.3新添加的編譯宏,defineModel來使用,每種方案結(jié)合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-10-10
一文帶你深入了解V-model實現(xiàn)數(shù)據(jù)雙向綁定
這篇文章主要為大家詳細介紹了V-model實現(xiàn)數(shù)據(jù)雙向綁定的相關知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2024-12-12

