微信小程序 request接口的封装实例代码
2017-04-26 16:33
871 查看
小程序request接口的封装(本质上是对request回调函数再次回调)
module.exports.getData = function (url) { var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var method = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'POST'; var header = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }; return new Promise(function (resolve, reject) { wx.request({ url: url, data: data, method: method, header: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }, success: function (res) { resolve(res) }, fail: function (res) { reject(res) } }) }) }
调用方法
var common = require("../../common/common.js") common.getData(postUrl) .then(function(data){ this.setData({ //.... }); console.log(data); });
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
您可能感兴趣的文章:
相关文章推荐
- 微信小程序 使用picker封装省市区三级联动实例代码
- 微信小程序 http请求封装详解及实例代码
- 微信小程序封装http访问网络库实例代码
- 微信小程序 http请求封装详解及实例代码
- 微信小程序 wx.request方法的异步封装实例详解
- 浅谈Java代码的 微信长链转短链接口使用 post 请求封装Json(实例)
- 微信小程序对wx.request函数进行封装操作的代码示例
- 微信小程序request请求后台接口php的实例详解
- 【微信小程序】网络请求接口封装实例
- 微信小程序 wx.request(object) API详解及实例代码
- 微信小程序 wx.request(接口调用方式)详解及实例
- 微信小程序request请求后台接口php简单实例
- 微信小程序wx.request接口封装
- 微信小程序 获取微信OpenId详解及实例代码
- 微信小程序 五星评分(包括半颗星评分)实例代码
- 微信小程序 action-sheet详解及实例代码
- 微信小程序 POST请求(网络请求)详解及实例代码
- 微信小程序 scroll-view组件实现列表页实例代码
- 微信小程序request请求实例,网络请求。
- 微信小程序 require机制详解及实例代码