uni-app学习笔记三十--request网络请求传参
request用于发起网络请求。
OBJECT 参数说明
参数名 | 类型 | 必填 | 默认值 | 说明 | 平台差异说明 |
---|---|---|---|---|---|
url | String | 是 | 开发者服务器接口地址 | ||
data | Object/String/ArrayBuffer | 否 | 请求的参数 | App 3.3.7 以下不支持 ArrayBuffer 类型 | |
header | Object | 否 | 设置请求的 header,header 中不能设置 Referer | App、H5端会自动带上cookie,且H5端不可手动修改 | |
method | String | 否 | GET | 有效值详见下方说明 | |
timeout | Number | 否 | 60000 | 超时时间,单位 ms | H5(HBuilderX 2.9.9+)、APP(HBuilderX 2.9.9+)、微信小程序(2.10.0)、支付宝小程序 |
dataType | String | 否 | json | 如果设为 json,会对返回的数据进行一次 JSON.parse,非 json 不会进行 JSON.parse |
success | Function | 否 | 收到开发者服务器成功返回的回调函数 | ||
fail | Function | 否 | 接口调用失败的回调函数 | ||
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
示例代码
<script setup>uni.showLoading()uni.request({url:"https://jsonplaceholder.typicode.com/posts",data:{"id":1,"userId":1},header:{token:"adofoaffdd","content-type":"application/json"},method:'POST',timeout:10000,success:res=>{console.log(res)},fail:err=>{console.log("网络请求超时")console.log(err)},complete: () => {uni.hideLoading()}})
</script>
更多参数使用方法详见官方文档
uni.request(OBJECT) | uni-app官网