例如我現在的項目運行在 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