小程序:uni学习第五弹之请求 | 缓存
1. 请求
- uni.request(object)
- 参数
- url
- data:请求的参数
- header:设置请求的 header,header 中不能设置 Referer
- method
- timeout:超时时间,单位 ms
- dataType
- responseType:设置响应的数据类型
- success:data | statusCode | header
- fail
- complete
- sslVerify:验证 ssl 证书
- withCredentials:跨域请求时是否携带凭证(cookies)
- firstIpv4:DNS解析时优先使用ipv4
- Tips
- 请求的 header 中 content-type 默认为 application/json
- 网络请求的 超时时间 可以统一在 manifest.json 中配置
2. 缓存 storage
- 设置:uni.setStorage(object)
object
- key
- data
- success:接口调用成功的回调函数
- fail:接口调用失败的回调函数
- complete:接口调用结束的回调函数(调用成功、失败都会执行)
- 获取:uni.getStorage( object)
object:
- key
- success:接口调用成功的回调函数
- fail:接口调用失败的回调函数
- complete:接口调用结束的回调函数(调用成功、失败都会执行)
- 删除:uni.removeStorage('key')
object
- key
- success:接口调用成功的回调函数
- fail:接口调用失败的回调函数
- complete:接口调用结束的回调函数(调用成功、失败都会执行)
- 清理:uni.clearStorage()
- 获取当前 storage 的相关信息:uni.getStorageInfo(object)
- object
- success:接口调用成功的回调函数
- keys:string[] | 当前 storage 中所有的 key
- currentSize:number | 当前占用的空间大小, 单位:kb
- limitSize: number | 限制的空间大小, 单位:kb
- fail:接口调用失败的回调函数
- complete:接口调用结束的回调函数(调用成功、失败都会执行)
- **sync: 同步接口**
- 设置:uni.setStorageSync('key', data)
- 获取:uni.getStorageSync('key')
- 删除:uni.removeStorageSync('key')
- 清理:uni.clearStorageSync()
- 获取当前 storage 的相关信息:uni.getStorageInfoSync()