微信小程序连接后端案例

setting.js

小Demo测试一下(楼主这里使用的为PHP后台):

微信开发者工具中写入如下代码,

// pages/setting/setting.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    demo:''
  },


  ceshifuwuqi:function(){
    
    var that = this
    wx.request({
      url: `http://127.0.0.1:8081/sql/sql310.php`,//你的后台url地址
      data:{
        name:'微信'
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      success(result) {
        console.log(result);
        that.setData({
          demo: result.data
        })
      },
      fail(error) {
        util.showModel('请求失败', error);
        console.log('request fail', error);
      }
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})

setting.wxml



  
  点击测试服务器
  {{demo}}
  

后台代码:


微信小程序连接后端案例_第1张图片微信小程序连接后端案例_第2张图片

微信小程序连接后端案例_第3张图片

你可能感兴趣的:(微信小程序,小程序)