Vue项目与nginx部署跨域问题

在使用Vue构建的前端工程中,访问本地不同ip与端口,都会存在跨域问题,下面给出一个使用代理解决的办法:

    在config目录的index.js文件中,添加:

    proxyTable: {
      '/api':{
        target:'https://api.douban.com',
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }
    }

在要访问该地址的时候这样写:

axios.get('/api/v2/book/1220562').then(resp=>{
   console.log(resp)
})

Vue项目部署到nginx上的跨域问题解决

这还没完,现在我们要将项目部署到nginx上,此时原来可以访问的接口又访问不到了,所以这个时候还要对nginx进行设置。

location /api {			
	proxy_pass https://api.douban.com/;						
} 

在nginx.conf文件的server节点下,加上以上代码即可。

你可能感兴趣的:(Vue)