Home >WeChat Applet >Mini Program Development >Introduction to the encapsulation of the WeChat applet request interface

Introduction to the encapsulation of the WeChat applet request interface

不言
不言Original
2018-06-27 13:59:392887browse

This article mainly introduces the relevant information about the encapsulation example code of the WeChat applet request interface. Friends who need it can refer to

The encapsulation example code of the WeChat applet request interface

Encapsulation of the mini program request interface (essentially calling back the request callback function again)

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)
  }
 })
 })
}

Calling method

var common = require("../../common/common.js")
common.getData(postUrl)
 .then(function(data){
  this.setData({
  //....
  });
  console.log(data);
 });

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Introduction to the WeChat Mini Program network request wx.request

Video API in the WeChat Mini Program Parse

The above is the detailed content of Introduction to the encapsulation of the WeChat applet request interface. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn