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

本节知识点

  • 展示小程序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('用户点击取消')
            }
          }
        })
      }
    })
  },
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,973评论 19 139
  • 转载链接 注:本文转载知乎上的回答 作者:初雪 链接:https://www.zhihu.com/question...
    pengshuangta阅读 28,772评论 9 295
  • Spring Web MVC Spring Web MVC 是包含在 Spring 框架中的 Web 框架,建立于...
    Hsinwong阅读 22,584评论 1 92
  • 二十岁的天空,听起来很有诗意,但对他来说,并不是。他算不上可怜,因为他没什么值得可怜的地方,最起码在别人看起来是这...
    时影书阅读 256评论 0 2
  • 在悟空问答的搬运,在这里跟大家分享。 工作中能力很强的人有哪些共同的特征? 我是职场老司机佟将军,职场履历从基层到...
    大女主研习社阅读 3,826评论 0 1