Home  >  Article  >  WeChat Applet  >  POST request for small program development

POST request for small program development

巴扎黑
巴扎黑Original
2017-09-12 09:35:461469browse

There are indeed a lot of pitfalls in the previous post requests. The mini program has been updated since the public beta, so the previous version can only be discarded and written again

There are not many get requests for the mini program I said it, because the documents all have examples of ctrl+c ctrl+v.

POST request for small program developmentFor POST requests, it is enough to pay attention to two points now

1. Add method: "POST", case-insensitive
2-1. For ordinary parameter transfer, add 'content-type': "application/x-www-form -urlencoded" (The content-type here has the same effect as Content-Type. It had to be written in all lowercase before)
2-2. When passing json object parameters, you need to add 'content-type': " application/json"

POST request for small program developmentThen you can go directly to the example:

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 class="container"> 
  <button bindtap="user2json">bean2json</button>  
  <text>id:{{id}}</text>  
  <text>username:{{username}}</text>  
  <text>age:{{age}}</text>
</view>

The above is the detailed content of POST request for small program development. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn