vue4 axios proxy cross-domain

Common cross-domain solutions
common front-end cross-domain solutions (full)

vue cli webpack related

The devServer.proxy configuration of vue cli

Axios Chinese description
Insert picture description here
New file vue.config.js

module.exports = {
    
    
  devServer: {
    
    
    proxy: {
    
    
      '/api': {
    
    // 使用"/api"来代替"http://f.apiplus.c"
        // target: 'http://192.168.0.102:8080', // 源地址
        target: 'http://118.178.84.155:5050/api', // 源地址
        changeOrigin: true, // 改变源
        pathRewrite: {
    
    
          '^/api': '' // 路径重写
        }
      }
    }
  }
}

this.$axios.get('/api/4g/status').then((res) => {
    
    
      console.log(res)
    })

split line

Here must pay attention to why the red wavy line keeps bursting, because eslints is turned on, there is a problem with the format. After
Insert picture description here
searching for a long time, I found that it was a space problem. Only two spaces were left and the tab key was four.

I am especially grateful to my brother Wang,
originally to solve the cross-domain problem

It’s that
Insert picture description here
the address of this server is different from the local address, and the browser’s protection strategy cannot be accessed by different sources. I
’m not going to describe what is the same origin, etc.,
just record the process of the occurrence and resolution of the problem.

Baidu has learned a lot about how vue solves cross-domain and so on, but the answers found are not the answers you want

Insert picture description here
Asked the front side, did not get the appropriate answers, finally Wang and powerful
he threw me a link
he made me a global search proxy
search devServer
then find the corresponding configuration on where
I went looking for the official website, which is the most The first two official
websites were fixed by me later, but it’s not difficult.
Insert picture description here
Anyway , I’m happy, and I went to do other things.

Guess you like

Origin blog.csdn.net/fengtingYan/article/details/103386384