微信小程序发送数据和接收数据

本节知识点

  • 展示小程序get请求和post请求

(一) 小程序get请求

  • wxml代码

  • js代码
douban: function () {
  var _this = this;
   wx.request({
     url: 'xxxxxxx', /*写地址*/
     header: {
       'content-type': 'json' // 默认值
     },
     success: function (res) {
       console.log(res);
       var imgsrc=res.data.image;
       _this.setData({
         imgsrc1:imgsrc
       })
     }
   })
 },

(二)小程序POST请求

  • html代码

  • js代码
 fashe: function () {
    var _this = this;
    wx.request({
      url: 'http://localhost:3000/post',
      header: { "content-type": "application/x-www-form-urlencoded" },
      method:"POST",
      data: {
        name: "新开始",
        sex: "密码"
      },
      success: function (res) {
        console.log(res.data);
        var result = res.data.name;
        console.log(result);
        wx.showModal({
          title: '这是收到的数据',
          content: JSON.stringify(res),
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定');
              _this.setData({
                jiekou:result
              })
           
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    })
  },

你可能感兴趣的:(微信小程序发送数据和接收数据)