Home > Article > WeChat Applet > Mini program development--wx.request asynchronous encapsulation example tutorial
This article mainly introduces the relevant information about the asynchronous encapsulation example of the wx.request method of the WeChat applet. Friends in need can refer to
wx-promise-request is a reference to the WeChat applet Asynchronous wrapper of wx.request method.
Solve the problem
Support Promise (using the es6-promise library).
Manage the request queue and solve the problem of error reporting when the maximum number of concurrent requests exceeds 10.
Download
npm install wx-promise-request
and then copy the dist/index.js file to your mini program project.
Use
import {request} from './wx-promise-request'; request({ url: 'test.php', data: { x: '', y: '', }, header: { 'content-type': 'application/json', }, }) .then(res => console.log(res)) .catch(error => console.error(error))
setConfig(object)
Configure wx-promise-request through setConfig, such as: use qcloud Provided request method; use other Promise libraries, etc.
import {request, setConfig} from './wx-promise-request'; import qcloud from './vendor/qcloud-weapp-client-sdk/index'; import Promise from 'bluebird'; setConfig({ request: qcloud.request, Promise, }) request({ url: 'test.php', }) .then(res => console.log(res)) .catch(error => console.log(error));
【Related recommendations】
1. Complete source code download of WeChat mini program
2. WeChat mini program demo: Kaka Auto
3. Simple left swipe operation and waterfall flow layout
The above is the detailed content of Mini program development--wx.request asynchronous encapsulation example tutorial. For more information, please follow other related articles on the PHP Chinese website!