vue-cli3.0解决跨域问题(超简单)

  1. 在根目录新建文件:vue.config.js
    vue-cli3.0解决跨域问题(超简单)_第1张图片
  2. 在文件新增内容:
module.exports = {
    publicPath: '/',
    outputDir: 'dist',
    devServer: {
        open: true,
        host: 'localhost',
        port: '8081',
        proxy: {
            '/api': {
                target: 'http://localhost:8080', // 要请求的地址
                ws: true,
                changeOrigin: true,
                pathRewrite: {
                    '^/api': ''
                }
            }
        }
    }
};

其中,http://localhost:8080是要请求的地址,可根据自己的需求更换。这样配置之后,如果要请求http://localhost:8080/getFile这个url,只需像下面这样写ajax请求即可:

 this.axios.get('/api/getFile', {//用api代替http://localhost:8080
                }).then( response => {
                    window.console.log("成功" + response)
                }).catch(
                error => {
                    window.console.log("失败"+error)
                })

你可能感兴趣的:(Vue.js)