跨域之接口代理

1.在vue.config.js中配置如下:

module.exports={
      /* webpack-dev-server 相关配置 */
    devServer: {
        host:'localhost',
        port:8080,
        proxy:{
            '/api':{
                 /* 目标代理服务器地址 */
                target: 'https://www.imooc.com',
                 /* //changeOrigin是false:
                 请求头中host仍然是浏览器发送过来的host;
                 如果设置成true:发送请求头中host会设置成target。*/
                changeOrigin: true,
                pathRewrite:{//将api替换为空
                    '/api':''
                  }
            }
        }
    }
}

2.在项目中使用如下:

axios('/api/activity/servicetime').then((res)=>{
let result=res;
this.data=result.data;
window.console.log(this.data)
})
发布了79 篇原创文章 · 获赞 36 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/yezi__6/article/details/105535035