The applet calls the API to return data or submit data

Source: Internet
Author: User

Due to a small program initiating a network request through Wx.request

Document Address

Https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-request.html

Accustomed to use Ajax, then the (wx.request) package 1, the new file Http.js
/* Used Ajax, then the (wx.request) encapsulation, call Mode 1, first introduced: Const HTTP = require ('. /.. /js/http.js ') 2, how to use: Http.post or HTTP.GET3, params parameter format such as: {start:1, count:4}*//* Official document https://mp.weixin.qq.com/debug/ Wxadoc/dev/api/network-request.html*/module.exports = {  get (Apiurl, yes, error) {    wx.request ({      URL: Apiurl,      header: {' Content-type ': ' JSON '},      Success:yes,      fail:error    })  },  post (Apiurl, Params, yes, error) {    wx.request ({      url:apiurl,      data:params,      header: {' Content-type ': ' JSON '},< C13/>success:yes,      fail:error    })  }}

2. Page Call mode

/**   * Life cycle Function--Listen page load   */   onload:function (options) {        Const HTTP = require ('). /.. /js/http.js ')//introduction of http.js file       var params = {start:1, count:4};//Parameter      http.post ("Https://api.douban.com/v2/movie /coming_soon ", {start:1,count:4}, function (res) {        Console.log (" return result = "+json.stringify (res.data));      }, Function (e) {        console.log ("return error result =" + json.stringify (e));}      );   

  

The applet calls the API to return data or submit data

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.