微信小程序 request请求封装
在utils文件夹新建文件utils.js,封装代码如下:
小程序升级后内部不自带Promise方法,需外部引入Promise方法
var sendRequest = function (url, method, data = {}, header = {} )
{
var promise = new Promise(function (resolve, reject){
wx.request({
url: url,
data: data,
method: method,
header:header,
success: function(data) {
//做一些统一处理操作,例如401验证
//resolve用于具体调用中
resolve(data);
},
fail: function(data) {
reject(data);
}
})
})
return promise
}
//导入
module.exports = {
sendRequest : sendRequest
}
应用示例:
index.js页面
//加载util
const utils = require('../utils/util')
utils.sendRequest(your_request_url, 'GET', { user_id: user_id })
.then(function (response) {
that.setData({
likes: response.data.data
})
}, function (error) {
console.log(error);
})
微信小程序 request请求封装的更多相关文章
- 微信小程序request请求封装,验签
1/ 公共文件util添加 request请求 //简单封装请求 function request(params, path, isShowLoading = true, goBack = false ...
- 微信小程序request请求封装
var app = getApp(); function request(url,postData,doSuccess,doFail,doComplete){ var host = getApp(). ...
- mpvue学习笔记-之微信小程序数据请求封装
简介 美团出品的mpvue已经开源出来很久了,一直说要进行一次实践,这不最近一次个人小程序开发就用上了它. 看了微信官方的数据请求模块--request,对比了下get和post请求的代码,发现如果在 ...
- 微信小程序request请求的封装
目录 1,前言 2,实现思路 3,实现过程 3.1,request的封装 3.2,api的封装 4,实际使用 1,前言 在开发微信小程序的过程中,避免不了和服务端请求数据,微信小程序给我们提供了wx. ...
- 微信小程序request请求之GET跟POST的区别
1.GET 例子: wx.request({ url: 'test.php', //仅为示例,并非真实的接口地址 data: { x: '' , y: '' }, header: { 'content ...
- 微信小程序request请求动态获取数据
微信小程序开发文档链接 1 后台代码: clickButton:function(){ var that = this; wx.request({ url: 'http://localhost:909 ...
- 微信小程序request请求实例,网络请求。
最近微信小程序开始开放测试了,小程序提供了很多api,极大的方便了开发者,其中网络请求api是wx.request(object),这是小程序与开发者的服务器实现数据交互的一个很重要的api. 官方参 ...
- 微信小程序request(ajax)接口请求封装
微信小程序request(ajax)接口请求封装 最近在进行小程序的编写,需要调用后端接口,经常要用到wx.request方法,所以就自己封装了一下,简化一下代码,如果能给大家提供帮助更好,在封装的时 ...
- 微信小程序数据请求方法wx.request小测试
微信小程序数据请求方法 wx.request wxml文件: <view> <textarea value="{{textdata}}"/> </vi ...
随机推荐
- Eclipse和JDK的安装配置
工欲善其事,必先利其器.最近开始学习Java语言,必不可少的要先安装一个IDE,我选择了eclipse,下面我们讲讲如何来安装及配置. Step1:工具的下载 这里我们需要用到三个工具安装包,JDK. ...
- host 'xx' is not allowed to connect to this MySql server
update mysql.user set host = '%' where user = 'root'; FLUSH PRIVILEGES; select * from mysql.user;
- SVG中的元素属性
SVG attributes by category Animation event attributes onbegin, onend, onload, onrepeat Animation att ...
- fill & stroke
- (void)stroke Draws a line along the receiver’s path using the current drawing properties. - (void) ...
- webpack中Entry与Output的基础配置
entry顾名思义,就是打包的入口文件 module.exports = { // 这个文件要做打包,从哪一个文件开始打包 entry: './src/index.js', // 打包文件要放到哪里去 ...
- VIM之模式
1.模式介绍: 在真正开始使用VIM之前,你必须先了解VIM的模式,否则在 VIM 面前你可能会手足无措.VIM是有模式 编辑器,这意味着 VIM 有多种不同的工作模式,在不同的工作模式下用户相同的操 ...
- jquery mobile header title左对齐 button右对齐
<div data-theme="b" data-role="header" data-position="fixed"> &l ...
- 无网络环境下使用docker加载镜像
无网络环境下使用docker加载镜像 你需要做的主要有3步骤: 先从一个有网络的电脑下载docker镜像 [root@localhost ~]# docker pull hub.c.163.com ...
- MyBatis保存完整日期的解决方法
在用mybatis时,对mysql数据库是datatime字段添加值是,发现添加成功后查看数据库字段值是,只有年月日有值,时分秒则为0来表示的,更改为java.sql.date,time等也不行,如果 ...
- ios下元素溢出设置 overflow:auto; 不能滑动解决办法
ios下元素溢出设置 overflow:auto; 不能滑动解决办法: overflow:auto; overflow-y:scroll; -webkit-overflow-scrolling:tou ...