Various writing methods of wx.request()

post request, parameter is key value pair format

    wx.request({
      url: 'http://192.168.1.103/yiji/skillList.php',
      method: 'POST',
      data:'pageSize=1&pageNum=10',    //Parameter is key value pair string
      header: {
        //Set the parameter content type to x-www-form-urlencoded
        'content-type':'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: function (res) {
        console.log(res.data)
        that.setData({
          items: res.data
        })
      }

    })
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

POST request, parameter is json format

Please refer to the official example directly

wx.request({
  url: 'test.php', //Example only, not real interface address
  data: {          //Parameter is json format data
     x: '' ,
     y: '',
     z:12
  },
  header: {
      //Set the parameter content type to json
      'content-type': 'application/json'
  },
  success: function(res) {
    console.log(res.data)
  }
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

When wx.request() gets the response

Page({
  data: {
    motto: 'wzh ... '
  },
  //Event handler
  onLoad: function (options) {
    var that = this

    //Skill information list
    wx.request({
      url: 'http://192.168.1.103/yiji/skillList.php',
      method: 'POST',
      data:'pageSize=1&pageNum=10',
      header: {
        'content-type':'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: function (res) {
        console.log(res.data)
        //In this way, the data cannot be transferred. You must use the setData() method
        //that.data.items = res.data ;
        //The official document states that the setData() method must be used to transfer the data
        that.setData({
          items: res.data
        })
      }

    })
  }
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

Reference resources

  1. Official wechat original

    Page.prototype.setData() 
    The setData function is used to send data from the logical layer to the view layer and change the corresponding value of this.data.  
    setData() parameter format
    Accept an object and change the value corresponding to the key in this.data to value in the form of key and value.  
    The key can be given in the form of data path, such as array[2].message, a.b.c.d, and does not need to be defined in this.data in advance.  
    Note:
    Modifying this.data directly without calling this.setData will not change the status of the page, and will cause data inconsistency
    The data set in a single time cannot exceed 1024kB. Please try to avoid setting too much data at a time.

  2. The data requested by wechat app wx.request was not displayed on the page in time - netizen

 onLoad: function () {
          var that = this;
      wx.request({
        url:app.globalData.url.api.home,
        success: function(res) {
            var matchsFirst = xxx;
            var matchsSecond= xxx;
            var matchsLast= xxx;
            //This will not render the data to the page, but it is OK in version 0.9   
            that.data.matchsFirst=matchsFirst;
            that.data.matchsSecond=matchsSecond;
            that.data.matchsLast=matchsLast;
        }});
    };

//It should be assigned in this way
that.setData({
          matchsFirst:matchsFirst,
          matchsSecond:matchsSecond,
          matchsLast:matchsLast
        });

Keywords: JSON PHP

Added by whisher06 on Wed, 01 Apr 2020 13:21:28 +0300