vue前后端端口不一致的問(wèn)題解決
在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', //需要更改的后端的端口號(hào)
changeOrigin: true,
pathRewrite: {
'^/api': '/'
}
}
},
這里的配置是正則表達(dá)式,以/api開(kāi)頭的將會(huì)被用用‘/api'替換掉,假如后臺(tái)文檔的接口是 /api/list/xxx
//前端api接口寫(xiě):axios.get('/api/list/xxx') , 被處理之后實(shí)際訪問(wèn)的是:http://news.baidu.com/api/list/xxx
}
}},解決前后端不同端口號(hào)的跨域請(qǐng)求問(wèn)題
擬定前端端口號(hào)8000;后端端口號(hào)是8070
前端使用的Vue框架,默認(rèn)數(shù)據(jù)信息存儲(chǔ)在 .eve.development中,需要配置(修改)前端數(shù)據(jù)發(fā)送的路徑
NODE_ENV=development //設(shè)置VUE_APP_PREVIEW=false , VUE_APP_PREVIEW=false //URL后接后端tomcat服務(wù)地址http://localhost:8070/ VUE_APP_API_BASE_URL=http://localhost:8070/
方式1,在后端啟動(dòng)文件同級(jí)目錄,創(chuàng)建一個(gè)目錄Corsconfig
package com.mashibing.config;
//當(dāng)通過(guò)文件配置跨域請(qǐng)求時(shí),則配置的是全局
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();
// 設(shè)置屬性
// 允許跨域請(qǐng)求的地址,*表示所有
corsConfiguration.addAllowedOrigin("*");
// 跨域的請(qǐng)求頭
corsConfiguration.addAllowedHeader("*");
// 跨域的請(qǐng)求方法
corsConfiguration.addAllowedMethod("*");
// 在跨域請(qǐng)求的時(shí)候使用同一個(gè) Session
corsConfiguration.setAllowCredentials(true);
return corsConfiguration;
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//配置 可以訪問(wèn)的地址
source.registerCorsConfiguration("/**", buildConfig());
return new CorsFilter(source);
}
}
方式2,通過(guò)注解的方式,在需要和前端交互數(shù)據(jù)的頁(yè)面配置注解@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 "";
}
}到此這篇關(guān)于vue前后端端口不一致的問(wèn)題解決 的文章就介紹到這了,更多相關(guān)vue前后端端口不一致內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue 2.0 中依賴(lài)注入 provide/inject組合實(shí)戰(zhàn)
這篇文章主要介紹了Vue 2.0 依賴(lài)注入 provide/inject組合,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-06-06
vue中使用element ui的input框?qū)崿F(xiàn)模糊搜索的輸入框
這篇文章主要介紹了vue中使用element ui的input框?qū)崿F(xiàn)模糊搜索的輸入框,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11
vue3中使用v-model實(shí)現(xiàn)父子組件數(shù)據(jù)同步的三種方案
這篇文章主要介紹了vue3中使用v-model實(shí)現(xiàn)父子組件數(shù)據(jù)同步的三種方案,如果只有一個(gè)匿名v-model的傳遞的話(huà),可以使用vue3.3新添加的編譯宏,defineModel來(lái)使用,每種方案結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10
一文帶你深入了解V-model實(shí)現(xiàn)數(shù)據(jù)雙向綁定
這篇文章主要為大家詳細(xì)介紹了V-model實(shí)現(xiàn)數(shù)據(jù)雙向綁定的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-12-12
vue表格顯示字符串過(guò)長(zhǎng)的問(wèn)題及解決
這篇文章主要介紹了vue表格顯示字符串過(guò)長(zhǎng)的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10

