基于Promise封装uni-app的request方法,实现类似axios形式的请求
https://my.oschina.net/u/2428630/blog/3004860
uni-app框架中
安装(项目根目录下运行)
npm install uni-request --save
文件中引用
import uniRequest from 'uni-request';
使用方法
请求方法的别名
uniRequest.request(config)
uniRequest.get(url[, config])
uniRequest.delete(url[, config])
uniRequest.head(url[, config])
uniRequest.options(url[, config])
uniRequest.post(url[, data[, config]])
uniRequest.put(url[, data[, config]])
uniRequest.patch(url[, data[, config]])
全局配置
uniRequest.defaults.baseURL = 'https://yourapi.domain.com';
uniRequest.defaults.headers.common['Authorization'] = AUTH_TOKEN;
uniRequest.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
发送get请求
// 向具有给定ID的用户发出请求
uniRequest.get('/user?id=12345')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
// 可选地,上面的请求也可以按照
uniRequest.get('/user', {
data: {
id: 'number'
}
}).then(function (response) {
console.log(response);
}).catch(function (error) {
console.log(error);
});
// 想要使用 async/await? 将`async`关键字添加到外部函数/method
async function getUser() {
try {
const response = await uniRequest.get('/user?ID=12345');
console.log(response);
} catch (error) {
console.error(error);
}
}
发送post请求
uniRequest.post('/user', {
firstname : 'firstname',
lastname : 'lastname'
}).then(function (response) {
console.log(response);
}).catch(function (error) {
console.log(error);
});
以上就是基本用法,如果掌握了就可以使用了uni-request
基于Promise封装uni-app的request方法,实现类似axios形式的请求的更多相关文章
- vue 使用 axios 时 post 请求方法传参无法发送至后台
axios 时 post 请求方法传参无法发送至后台报错如下 Response to preflight request doesn't pass access control check: No ' ...
- 基于promise对小程序http请求方法封装
原因是我不想每次请求都复制粘贴那么长的请求地址,所以我把前边那一坨请求地址作为基础地址,只传后台给的路由就ok,而且,并不是每次请求都要显示正在加载,这对小程序体验很差,所以,我加了个形参,用来判断是 ...
- 微信小程序ES6方法Promise封装接口
为何要封装接口? 有小程序开发的经验者,相信对微信API Request很熟悉了.对接接口时,有大部分的开发者都是直接调用request方法,去请求后台接口并渲染数据.诚然,直接使用api发起请求对接 ...
- promise封装微信小程序的request
1.在utils下创建一个 request.js文件,然后将方法导出 const app = getApp(); //使用promise封装request请求 const POST = (url, p ...
- uniapp封装uni.request请求
封装一个uniapp请求 新建一个http.js文件封装uni.request const BASE_URL = process.env.NODE_ENV === 'development' ? '' ...
- 简单的基于promise的ajax封装
基于promise的ajax封装 //调用方式: /* ajaxPrmomise({ url:, method:, headers:{} }).then(res=>{}) */ ;(functi ...
- 20191011-构建我们公司自己的自动化接口测试框架-Action的request方法封装
Action模块 封装接口request方法,根据传入的参数调用不同的请求方法,因为项目特色,我们公司的接口都是get和post方法,所以仅仅封装了get和post方法: import request ...
- vue3 专用 indexedDB 封装库,基于Promise告别回调地狱
IndexedDB 的官网 https://developer.mozilla.org/zh-CN/docs/Web/API/IndexedDB_API 这个大概是官网吧,原始是英文的,现在陆续是出中 ...
- React Native 网络请求封装:使用Promise封装fetch请求
最近公司使用React作为前端框架,使用了异步请求访问,这里做下总结: React Native中虽然也内置了XMLHttpRequest 网络请求API(也就是俗称的ajax),但XMLHttpRe ...
随机推荐
- VS2010 开发 VB6.0 activeX控件 dll
项目源码 https://download.csdn.net/download/csdn_z_s/10427764 开发环境 操作系统: win7 64位 旗舰版 Java语言开发环境: Eclip ...
- 论文阅读笔记四十六:Feature Selective Anchor-Free Module for Single-Shot Object Detection(CVPR2019)
论文原址:https://arxiv.org/abs/1903.00621 摘要 本文提出了基于无anchor机制的特征选择模块,是一个简单高效的单阶段组件,其可以结合特征金字塔嵌入到单阶段检测器中. ...
- SqlServer中创建非聚集索引和非聚集索引
聚集索引与非聚集索引,其实已经有很多的文章做过详细介绍. 非聚集索引 简单来说,聚集索引是适合字段变动不大(尽可能不出现Update的字段).出现字段重复率小的列,因为聚集索引是对数据物理位置相同的索 ...
- Punycode
Punycode是一个根据RFC 3492标准而制定的编码系统,主要用于把域名从地方语言所采用的Unicode编码转换成为可用于DNS系统的编码 “中文域名”不被标准的解析服务器支持,需转化为Puny ...
- [方案]基于Zynq WiFi方案构建
基于Zynq系列,搭建无线传输平台 1) 2.4G 2) 5G AC
- linux 查看java的安装路径
在linux下,如何找java的安装路径 han@ubuntu:/etc$ whereis java java: /usr/bin/java /usr/share/java /usr/lib/jvm/ ...
- Win10环境下载安装MySQL Community 8.0.12
1.下载MySQL Community 8.0.12的免安装版,下载地址:https://dev.mysql.com/downloads/mysql/ 2.解压到D:\Program Files\My ...
- 圆周率pi π 与 角度的对应关系
圆周率pi π 与 角度的对应关系 π 180° π/2 90° π/4 45° π/6 30°
- MERGE INTO无法更新ON中的字段解决办法
可以将on里的条件放到update 之后的where条件里 MERGE INTO xshtest.WEB_USER_VIP T1 USING ( select 53254624 enterpriseI ...
- Windows Server 2008 R2 修改远程桌面服务RDP默认端口及相应的防火墙配置
修改以下两个注册表项当中的默认端口3389为自定义端口: [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\Wd ...