PHP前端开发

小程序开发之POST请求

百变鹏仔 3天前 #前端问答
文章标签 程序开发

之前的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

<?xml  version="1.0" encoding="utf-8"?><view>   <button>bean2json</button>    <text>id:{{id}}</text>    <text>username:{{username}}</text>    <text>age:{{age}}</text></view>