欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue前后端端口不一致的問題解決

 更新時間:2023年10月24日 09:24:18   作者:木..木  
本文主要介紹了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
//設(shè)置VUE_APP_PREVIEW=false ,
VUE_APP_PREVIEW=false 
//URL后接后端tomcat服務(wù)地址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();
        // 設(shè)置屬性
        // 允許跨域請求的地址,*表示所有
        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 "";
    }
}

到此這篇關(guān)于vue前后端端口不一致的問題解決 的文章就介紹到這了,更多相關(guān)vue前后端端口不一致內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:

相關(guān)文章

最新評論