參考文章:uniapp uView1.0跨域h5配置多個代理
官方手冊:http 請求
項目中使用:
參考其他博主的文章是在manifest.json中配置代理,但在官方的手冊中是直接在script請求的,我嘗試請求了下沒問題,上線后也不存在跨域問題,數據直接拿到了(有點懵)
官方手冊中是在onload中請求的,實際項目中我是放在created中也ok。
下面附上官方請求get、post的示例
<script>export default {onLoad() {// 不帶headerthis.$u.post('http://www.example.com', {id: 3,menu: 1}).then(res => {console.log(res);});// 帶上header(對象形式),由于header為第三個參數,如果不需要請求參數,第二個參數傳一個空對象"{ }"即可this.$u.get('http://www.example.com', {}, {token: 'xyz'}).then(res => {console.log(res);});}}
</script>