微信小程序 怎么向后端发送请求??

js  文件中 代码

 /**
 * 生命周期函数--监听页面加载
 */
  onLoad: function (options) {
    var thit = this
    wx.request({   //请求地址
      url: 'http://127.0.0.1:8000/ss/w/',
      method: 'get',    
     // data:{},//没有数据可以不写 
      header: {  //请求头
        'content-type': 'application/json'
        // "Content-Type": "application/x-www-form-urlencoded"
      },

      //如果在sucess直接写this就变成了wx.request()的this了
      success: function (res) {
        // res.data相当于ajax里面的data,为后台返回的数据
        //打印后台返回的数据
        console.log(res.data.message)
        //直接把后台返回的数据 赋值给names 就可以直接调用names了
        thit.setData({ names: res.data.message })
      }
    })
  },
原文地址:https://www.cnblogs.com/pp8080/p/11985044.html