小程序(九) 小程序get请求和post请求

本节知识点

  • 展示小程序 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('用户点击取消')
            }
          }
        })
      }
    })
  },

文章作者: 雾烟云
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 雾烟云 !
  目录