微信小程序请求数据

微信小程序请求数据,在页面展示,可以在onLoad生命周期中进行请求。

1.新建目录http,新建文件http.js

2.在js文件中暴露需要使用的变量

var baseUrl = 'http://101.89.144.168';
//暴露出要使用的变量/接口/方法 ,model.exports
module.exports = {
  "getContentsList": baseUrl + "/api/portals/content/getContentsList",//列表接口
}

 3.设置是否校验请求链接。不设置这个,可能会请求失败。

4.在生命周期onLoad中使用wx.request({})请求数据require()方法引入需要请求js

onLoad: function (options) {
      //引入外部js
    var getContentsList = require("../../http/http.js");
    console.log(getContentsList)
    var indexBanner = { "type": "news_normal", "offset": 0, "limit": -1};
      wx.request({
        url: getContentsList.getContentsList,
        data: indexBanner,
        method:"post",// 用的是method
        header: {
         "Content-Type": "application/x-www-form-urlencoded"
        },
        success:function(res){
          console.log(res);
        }
      })
  },
扫描二维码关注公众号,回复: 5267662 查看本文章

猜你喜欢

转载自www.cnblogs.com/luguankun/p/10417254.html