基于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 ...
随机推荐
- Django—入门
索引 1.搭建环境 2.创建项目 3.设计模型 4.管理站点 5.视图及URL 6.模板 软件框架 问题1:什么是软件框架? 举个简单的例子,对于一个公司来说,公司中有各个职能部门,每个部门各司其职, ...
- James Munkres Topology: Sec 18 Exer 12
Theorem 18.4 in James Munkres “Topology” states that if a function \(f : A \rightarrow X \times Y\) ...
- Linux系统中yum 命令讲解
(一)yum介绍 Yum(全称为 Yellow dogUpdater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指定 ...
- zTree 3-- jQuery 树插件笔记
地址:http://www.treejs.cn/v3/demo.php#_507 数据结构json,里可以自定义属性. var zNodes =[ { name:"父节点1 - 展开&quo ...
- html表单通过关联数组向php后台传多条数据并遍历输出
通过表单向php后台传多条数据,以关联数组方式呈现,废话不多说,代码附上: html表单代码,方式我设置为get: <form action="php/cart.php" m ...
- python is, ==区别
“is” is the identity comparison. #比较引用是否相同 “==” is the equality comparison. #比较内容是否相同 >>> [ ...
- django——url(路由)配置
URL是Web服务的入口,用户通过浏览器发送过来的任何请求,都是发送到一个指定的URL地址,然后被响应. 在Django项目中编写路由,就是向外暴露我们接收哪些URL的请求,除此之外的任何URL都不被 ...
- Python 中的单例模式
单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. ...
- idea搜索jar中的类
ctrl+n快捷键
- linux进阶指令
1.df 查看磁盘空间 2.free 查看内存使用 -m 表示以mb位单位查看 total 总大小 used 使用过的空间 free 空闲的空间 shared 共享内存 buffer ...