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形式的请求的更多相关文章

  1. vue 使用 axios 时 post 请求方法传参无法发送至后台

    axios 时 post 请求方法传参无法发送至后台报错如下 Response to preflight request doesn't pass access control check: No ' ...

  2. 基于promise对小程序http请求方法封装

    原因是我不想每次请求都复制粘贴那么长的请求地址,所以我把前边那一坨请求地址作为基础地址,只传后台给的路由就ok,而且,并不是每次请求都要显示正在加载,这对小程序体验很差,所以,我加了个形参,用来判断是 ...

  3. 微信小程序ES6方法Promise封装接口

    为何要封装接口? 有小程序开发的经验者,相信对微信API Request很熟悉了.对接接口时,有大部分的开发者都是直接调用request方法,去请求后台接口并渲染数据.诚然,直接使用api发起请求对接 ...

  4. promise封装微信小程序的request

    1.在utils下创建一个 request.js文件,然后将方法导出 const app = getApp(); //使用promise封装request请求 const POST = (url, p ...

  5. uniapp封装uni.request请求

    封装一个uniapp请求 新建一个http.js文件封装uni.request const BASE_URL = process.env.NODE_ENV === 'development' ? '' ...

  6. 简单的基于promise的ajax封装

    基于promise的ajax封装 //调用方式: /* ajaxPrmomise({ url:, method:, headers:{} }).then(res=>{}) */ ;(functi ...

  7. 20191011-构建我们公司自己的自动化接口测试框架-Action的request方法封装

    Action模块 封装接口request方法,根据传入的参数调用不同的请求方法,因为项目特色,我们公司的接口都是get和post方法,所以仅仅封装了get和post方法: import request ...

  8. vue3 专用 indexedDB 封装库,基于Promise告别回调地狱

    IndexedDB 的官网 https://developer.mozilla.org/zh-CN/docs/Web/API/IndexedDB_API 这个大概是官网吧,原始是英文的,现在陆续是出中 ...

  9. React Native 网络请求封装:使用Promise封装fetch请求

    最近公司使用React作为前端框架,使用了异步请求访问,这里做下总结: React Native中虽然也内置了XMLHttpRequest 网络请求API(也就是俗称的ajax),但XMLHttpRe ...

随机推荐

  1. br-lan、eth0、eth1及lo (转)

    如果你的设备含有不少于1个的LAN接口,那这个设备在不同的接口之间可能有一个被称为交换(switch)的特殊连接.大多数的内部构造如下图所示: Linux 系统下输入ifconfig命令,会有如下输出 ...

  2. hasOne、hasMany、belongsTo

    这里将hasOne.hasMany.belongsTo进行一个详细举例说明. 首先,这3个的大致中文意思: hasOne:有一个,加上主谓语应该是 ,A 有一个 B hasMany:有很多,A 有很多 ...

  3. 微信H5支付证书过滤

    在对接微信支付,退款的时候,遇到 Caused by: java.lang.RuntimeException: java.io.IOException: DerInputStream.getLengt ...

  4. 【微信小程序】 wx:if 与 hidden(隐藏元素)区别

    wx:if 与 hidden 都可以控制微信小程序中元素的显示与否. 区别: wx:if 是遇 true 显示,hidden 是遇 false 显示. wx:if 在隐藏的时候不渲染,而 hidden ...

  5. 倒影问题(reflect:below)

    这个例子灵感来源于实现一个登录框下方的倒影: .box { width: 300px; height: 200px; border: 1px solid #1f637b; -webkit-box-re ...

  6. yagmail 实现发邮件

    yagmail 实现发邮件 yagmail 可以更简单的来实现自动发邮件功能. github项目地址: https://github.com/kootenpv/yagmail 安装 pip insta ...

  7. request.getParameter和request.setAttribute/request.getAttribute

    https://blog.csdn.net/ryelqy/article/details/79230513 request.getQueryString https://blog.csdn.net/w ...

  8. 微信tinker 热修复

    Tinker 是微信官方的Android热补丁解决方案,它支持动态下发代码.So库以及资源,让应用能够在不需要重新安装的情况下实现更新.当然,你也可以使用Tinker来更新你的插件. github:h ...

  9. MVC 微信开发获取用户OpenID

    第一次开发微信版网页,对最重要的获取微信OpenId,特此记录下来 1.首先得有appid和appsecret . public class WeiXin { public static string ...

  10. __x__(7)0905第二天__HTML的发展

    HTML的发展 浏览器各个厂商有不同的标准,一个网页的兼容性非常差. 于是,W3C出来了,作为公益组织定义了HTML标准. 在 1993.6 实现并发布了第一个 HTML. 在 1995.11 开始创 ...