微信小程序中如何调用本地的接口

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sunshao904/article/details/86612837

小程序是运行在微信平台的一种轻量级app,通过一系列微信特定的组件构建用户界面,数据还是通过调用数据接口来获取。

所以,调用本地接口,在小程序中很重要。

1、首先需要绑定小程序id,然后添加request 合法域名,微信要求接口是https加密传输,所以服务器需要安装证书

2、在 onLoad中 使用微信 API wx.request 来请求

如果要用 POST 请求,需要在代码中设置 header: {'content-type': 'application/x-www-form-urlencoded'}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

  wx.request({

    url: 'https://api.xxx.com:9090/v1/Tools/UserModel/GetUserList/',

    data: {

      find: _find,

      tokenKey: _tokenKey,

      timeStamp: _timeStamp,

      currentPage: _currentPage,

    },

    method: "GET",

    header: {

      "Content-Type": "application/json",

    },

    success: function (res) {

      app.globalData.allData = res.data.datas;

      // console.log(res)

      self.setData({

        list: res.data.datas

      })

    },

    fail: function(){

      console.log("error")

    },

  })

},

猜你喜欢

转载自blog.csdn.net/sunshao904/article/details/86612837