vue框架是用vue-cli搭建起来的,今天遇到了跨域问题,是按照如下方式解决的:

在config下的index.js中,添加如下代码

proxyTable: {
      '/api': {
        target: 'http://localhost:8081/blog',
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }
    }

上面的写法,表示将/api前的请求替换为target的内容,比如,请求是"/api/getAllList",那么最终的请求地址为"http://localhost:8081/blog/getAllList"。

因此,在发送get或者post请求时,按照如下写法,就将当前请求映射到目标地址,实现跨域请求的目的。

getArticleData () {
      this.$http.get('/api/article/getArticles').then(response => {
        if (response.body.code === 0) {
          this.articleList = response.body.data.articles
        }
      }, res => {
        console.log('error')
      })
    }

 


本文转载:CSDN博客