美文网首页
微信小程序发送数据和接收数据

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

作者: 我拥抱着我的未来 | 来源:发表于2018-05-31 10:12 被阅读0次

本节知识点

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

(一) 小程序get请求

  • wxml代码
<button class="answer" bindtap="douban">测试豆瓣</button>
  • 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代码
<button class="answer" bindtap="fashe">发送数据</button>
  • 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('用户点击取消')
            }
          }
        })
      }
    })
  },

相关文章

网友评论

      本文标题:微信小程序发送数据和接收数据

      本文链接:https://www.haomeiwen.com/subject/runfsftx.html