微信小程序向服务端请求数据

版权声明:支持技术共享 https://blog.csdn.net/qq_38350907/article/details/82805608

荆轲刺秦王

微信的文档有说明:

具体用法:

getdata: function () {//定义函数名称

    var that = this;   // 这个地方非常重要,重置data{}里数据时候setData方法的this应为以及函数的this, 如果在下方的sucess直接写this就变成了wx.request()的this了

    var baseURL = "https://www.sscpark.com/index.php";
    var url = baseURL + "news/list";

    wx.request({

      url: url,//请求地址

      data: {  //发送给后台的数据,根据自己的服务器接收的数据而定

        openid: "oQlKA4q4VCztNBYq9fbDKAtgq",

        page: 1,

        flag: 0

      },
      header: {//请求头

        "Content-Type": "applciation/json"

      },
      method: "POST",//get为默认方法/POST

      success: function (res) {

        console.log(res.data);  //res.data相当于ajax里面的data,为后台返回的数据

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

          news:res.data.data.news  //动态改变 Data 里的数据

        })

      },

      fail: function (err) { },//请求失败

      complete: function () { }//请求完成后执行的函数

    })

  }

猜你喜欢

转载自blog.csdn.net/qq_38350907/article/details/82805608
今日推荐