【微信小程序】微信小程序与后台的交互

js

 onLoad: function (options) {
    var that = this;
    var list = that.data.list;
    var currentPage = that.data.currentPage;
    wx.showNavigationBarLoading(); //在标题栏中显示加载

  //调用读取数据库的方法
    this.getdata();

  },
  getdata: function () {//定义函数名称
    var that = this;   // 这个地方非常重要,重置data{}里数据时候setData方法的this应为以及函数的this, 如果在下方的sucess直接写this就变成了wx.request()的this了
    wx.request({
      url: 'http://localhost:8080/mallapp/app/searchlist',//请求地址
      /*data: {//发送给后台的数据
        name: "bella",
        age: 20
      },*/
      header: {//请求头
        "Content-Type": "applciation/json"
      },
      method: "GET",//get为默认方法/POST
      success: function (res) {
        console.log(res.data);//res.data相当于ajax里面的data,为后台返回的数据

        that.setData({//如果在sucess直接写this就变成了wx.request()的this了.必须为getdata函数的this,不然无法重置调用函数
            goods: res.data
        })

      },
      fail: function (err) { },//请求失败
      complete: function () { }//请求完成后执行的函数
    })
  },

wxml

<view  wx:for="{
   
   {goods}}" wx:for-item="value">
 <view class="text_top">{
   
   {value.goods_name}}</view>
</view>

猜你喜欢

转载自blog.csdn.net/wenyimutouren/article/details/79983565