Code example of the http request class encapsulated by WeChat applet

Source: Internet
Author: User
This document describes how to encapsulate network requests of applets. It has good reference value. Next, let's take a look at it. This article mainly introduces information about the encapsulation of small program network requests. It has good reference value. Let's take a look at it with the small editor.

I have previously written an article about small program Upload, pull, and refresh. Today I wrote about the encapsulation of small program network requests.

Here, we first declare a bug in the mini-program document, which causes the server to fail to receive the parameter during the request.

Sample code:

Wx. request ({url: 'Test. php ', // only for example. it is not the actual interface address data: {x: '', y:''}, header: {'content-type ': 'application/json'}, success: function (res) {console. log (res. data )}})

The Content-Type in the header must be in lower case for the server to receive parameters. It took me a long time to make it difficult to change the server. it turned out to be a problem. The request payload parameter cannot be received by the server.

function json2Form(json) {  var str = [];  for(var p in json){  str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));  }  return str.join("&"); }

In the end, we still think it is the content-type problem. In the end, I changed my mind and I thought that such a great team had made a very low-level mistake, and I had to make great strides for developers. Do not mention the code.

1. Http request class

Import util from 'util. js';/*** url request address * successful callback of success * failed callback of fail */function _ get (url, success, fail) {console. log ("------ start --- _ get ----"); wx. request ({url: url, header: {// 'content-type': 'application/json'}, success: function (res) {success (res );}, fail: function (res) {fail (res) ;}}); console. log ("---- end ----- _ get ----");}/*** url request address * successful callback of success * failed callback of fail */function _ post_from (url, data, success, fail) {console. log ("---- _ post -- start -------"); wx. request ({url: url, header: {'content-type': 'application/x-www-form-urlencoded',}, method: 'post', data: {data: data}, success: function (res) {success (res) ;}, fail: function (res) {fail (res) ;}}); console. log ("---- end ----- _ get ----");}/*** url request address * successful callback of success * failed callback of fail */function _ post_json (url, data, success, fail) {console. log ("---- _ post -- start -------"); wx. request ({url: url, header: {'content-type': 'application/json',}, method: 'post', data: data, success: function (res) {success (res) ;}, fail: function (res) {fail (res) ;}}); console. log ("---- end ---- _ post -----");} module. exports = {_ get: _ get, _ post: _ post, _ post_json: _ post_json}

2. test cases

2.1 get request

// GET mode let map = new Map (); map. set ('receiveid', '000000'); let d = json_util.mapToJson (util. tokenAndKo (map); console. log (d); var url1 = api. getBaseUrl () + 'searchtaskbyreceiveid? Data = '+ d; network_util. _ get (url1, d, function (res) {console. log (res); that. setData ({taskEntrys: res. data. taskEntrys}) ;}, function (res) {console. log (res );});

2.2 POST request

// Let map = new Map (); map in Post mode. set ('receiveid', '000000'); let d = json_util.mapToJson (util. tokenAndKo (map); console. log (d); var url1 = api. getBaseUrl () + 'searchtaskbyreceiveid'; network_util. _ post (url1, d, function (res) {console. log (res); that. setData ({taskEntrys: res. data. taskEntrys}) ;}, function (res) {console. log (res );});

The above is the details of the code instance for the http request class encapsulated by a applet. For more information, see other related articles in the first PHP community!

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.