例如我现在的项目运行在 http://localhost:8080,而我有个接口是 https://service.picasso.adesk.com/v1/wallpaper/album,发起请求就会出现跨域问题,以下是解决方法
首先找到 manifest.json 这个文件,在项目目录的src文件夹下,添加 "h5" 节点
1 "h5": { 2 "devServer": { 3 "port": 8080, 4 "disableHostCheck": true, 5 "proxy": { 6 "/dpc": { 7 "target": "https://service.picasso.adesk.com", 8 "changeOrigin": true, 9 "secure": true, 10 "pathRewrite": { 11 "^/dpc": "" 12 } 13 } 14 } 15 } 16 }
然后发起请求,注意:原来的 "https://service.picasso.adesk.com" 需要替换成 "/dpc"
2 url: ‘/dpc/v1/wallpaper/album‘, 3 success: (res) => { 4 console.log(res.data) 5 } 6 })
查看请求路径,如图,由 "https://service.picasso.adesk.com/v1/wallpaper/album" 变为了 "http://localhost:8080/dpc/v1/wallpaper/album",成功返回数据,问题解决 ~
参考文章:https://blog.csdn.net/paopao79085/article/details/91948809