微信小程序 怎么向后端發送請求??


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 })
      }
    })
  },


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM