Examples of asynchronous encapsulation of WeChat applet wx. request method are described in detail.

Source: Internet
Author: User

Examples of asynchronous encapsulation of the applet wx. request method are described in detail, and the applet wx. request

Wx-promise-request is an asynchronous encapsulation of the applet wx. request Method.

Solve the problem

Support Promise (using the es6-promise library ).

Manage request queues to solve the problem that an error is reported if the maximum number of requests is greater than 10.

Download

npm install wx-promise-request


Copy the dist/index. js file to your applet 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))

API

SetConfig (object)

Configure wx-promise-request through setConfig, such as using the request method provided by qcloud and using other Promise libraries.

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

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.