The WeChat applet encapsulates the code for accessing the network library through http, And the applet instance

Source: Internet
Author: User

The applet encapsulates the http access network library instance code, and the applet instance

Small Program encapsulate the code for accessing the network library through http

Previously, LeanCloud was used for storage. Now, the following encapsulation is used for calling traditional APIs:

Document Source: https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-request.html

The Code is as follows:

Var HOST = 'HTTP: // localhost/lendoo/public/index. php/'; // The website request interface, unified as postfunction post (req) {// initiate a network request wx. request ({url: HOST + req. uri, data: req. param, header: {"content-type": "application/x-www-form-urlencoded"}, method: 'post', success: function (res) {req. success (res. data)}, fail: function (res) {console. log (res) ;}} // export module. exports = {post: post}

Then the frontend call can be done as follows:

var http = require('../../utils/http.js');... http.post({   uri: http.orderListUri,   param: {   third_session: wx.getStorageSync('third_session')  },     success: function (data) {   that.setData({    orderList: data  });  } });

Generally, when you use an interface written by yourself, the method or header is agreed, so you do not need to write it again.

header: {   "content-type": "application/x-www-form-urlencoded"  },method: 'POST'

The fail callback method can also be processed in a unified manner. Further, the code value in the success callback can be further determined, and specific error codes can be processed in a unified manner, such as redirecting to the logon page.

After the above processing, is it concise?

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.