精华

微信小程序填坑之路之POST请求(更新2016/12/22)

  • • 发表于 8年前
  • • 作者 toBeMN
  • • 15601 人浏览
  • • 38 条评论
  • • 最后编辑时间 8年前
  • • 来自 [技 术]

原创声明:本文为作者原创,未经允许不得转载,经授权转载需注明作者和出处

之前的post的请求确实有不少的坑,公测以来小程序一直在更新,所以之前的版本只能作废掉,重新写一次

对于小程序的get请求就不多说了,因为文档都有例子ctrl+c ctrl+v就好了

而对于POST请求现在只注意两点就够了

1.加上 method: "POST",不区分大小写
2-1.普通参数传递需加上'content-type': "application/x-www-form-urlencoded"(这里的content-type跟Content-Type现在效果一样,之前得写成全小写)
2-2.json对象参数传递需加上'content-type': "application/json"

然后就可以直接上例子了:

index.js

var app = getApp()
Page({
  data: {
    id:'',
    username: '',
    age: ''
  },
  user2json: function() {
    var that=this;
    wx.request({
      url: 'http://localhost:8080/springMVC/user/bean2json.mn',
      data: {
        id:1,
        username:"toBeMN",
        age:38
      },
      method: 'POST', 
      header: {
        "Content-Type":"application/x-www-form-urlencoded"
      },
      success: function(res){
        that.setData({
          id:res.data.id,
          username: res.data.username,
          age: res.data.age
        })
      }
    })
  },
  onLoad: function () {}
})

index.wxml

<view class="container">
  <button bindtap="user2json">bean2json</button>
  <text>id:{{id}}</text>
  <text>username:{{username}}</text>
  <text>age:{{age}}</text>
</view>

效果

祝大家填坑顺利!!!

分享到:
38条评论
Ctrl+Enter
作者

toBeMN

toBeMN

APP:3 帖子:24 回复:59 积分:3193

已加入社区[2866]天

梦想成为全栈的男人

作者详情》
Top