当前位置:网站首页>CLI and vite realize cross domain through proxy
CLI and vite realize cross domain through proxy
2022-07-19 01:09:00 【Code programming】
One 、 Basic concepts
url Components : agreement :// domain name : port , When the target interface communicates with the current browser , As long as any one of the three changes , We all call it cross domain .
Two 、 problem
Use vue Project time , We use it most of the time axios, and axios We all configure baseUrl, Then the following error message appears !! This is caused by the browser's homology strategy Cross-domain problem , The same origin policy will prevent a domain from javascript The script interacts with the contents of another domain .


3、 ... and 、 The scaffold cli solve
vue Project address : http://localhost:8080
Back end interface : http://localhost:8888/test
(1) Create a new... Under the root directory vue.config.js file , And configure the following contents , When modifying the configuration file, remember to restart the project
module.exports = {
devServer: {
proxy: {
'/api': { // As long as there is api Just go here
target: 'http://localhost:8888/', // Destination address
changeOrigin: true, // Whether to enable cross domain
pathRewrite: {
'^/api': '' // take api Replace with ''
}
}
}
}
} (2)axios Secondary packaging
import axios from 'axios'
const request = axios.create({
baseURL: '/api',
timeout: 3000
})
export default request(3) stay App.vue Use in
<template>
<div id="app">
</div>
</template>
<script>
import request from './request/index.js'
export default {
name: 'App',
created() {
this.getData()
},
methods: {
// Get back-end data
async getData() {
let res = await request({
url: '/test',
method: 'GET',
})
console.log('res:', res)
}
}
}
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style> 
Four 、vite solve
vue Project address : http://localhost:5173
Back end interface : http://localhost:8888/test
(1) Find... In the root directory vite.config.js file , And write the relevant configuration , Remember to restart the project
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
server: {
proxy: {
'/api': { // As long as there is api Just go here
target: 'http://localhost:8888/', // Destination address
changeOrigin: true, // Whether to enable cross domain
rewrite: (path) => path.replace(/^\/api/, '')
}
}
}
})(2)axios Secondary packaging
import axios from 'axios'
const request = axios.create({
baseURL: '/api',
timeout: 3000
})
export default request(3) stay App.vue Use in , In fact, it is vue3 The grammar of
<template>
<div></div>
</template>
<script setup>
import request from './request';
import { onMounted } from 'vue'
// get data
const getData = async () => {
let res = await request({
url: '/test',
method: 'GET'
})
console.log('res:', res)
}
onMounted(() => {
getData()
})
</script>
<style scoped>
</style>
边栏推荐
- 如何解决图神经网络过相关?一个IBM的新视角!
- mysql数据表查询
- Food chain (DFS memorization)
- NVIDIA的jetson利用deepstream加速常见问题分析,deepstream消息发出外部接收,xvaier自启动程序,优秀的博客总结(精二)
- 2022.7.1
- Deep learning - multidimensional data and tensor
- By voting for the destruction of STI by Dao, seektiger is truly community driven
- 从编译器对指令集的要求看API设计原则
- Flowable工作流(flowable 数据库表结构)
- Jwt和Token
猜你喜欢

【MySQL】ERROR 1130 问题解决方案

Greenplum6.x客户端连接

Deep learning - multidimensional data and tensor

NiO three board axe buffer, a double-edged sword to improve program efficiency

12 张图,带你彻底理解 ZGC垃圾收集器!

The logical architecture of MySQL

LeetCode 242:有效的字母异位词

cmd中文乱码解决方案

Joint search set

Why processing data indicators
随机推荐
cmd中文乱码解决方案
Greenplum常用
Advanced Mathematics - Chapter 8 differential calculus of multivariate functions - extreme and maximum values of multivariate functions
为什么加工数据指标
The difference between TCP and UDP
QT打开外部程序并嵌入Qt界面
常见链表题及其 Go 实现
如何设计一个安全的对外接口
[MariaDB] solution: error 1045 (28000): access denied for user 'root' @ 'localhost' (using password: yes)
Fire prevention in midsummer! Guangdong carries out fire safety inspection in summer
How to solve the over correlation of graph neural network? A new perspective of IBM!
C 1 to 100 sum 9*9 multiplication table grade ABCDE
【JDBC】批处理思路
如何使用服务器搭建我们的博客
The problem of dynamically adding inputable rows in El table
[singleton mode] hungry, lazy, double lock security verification
Typora 怎么插入表格
关于目前流行的 Redis 可视化管理工具的详细评测
抓包工具哪家强(暴力窃取前戏)
【MariaDB】解决:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)