For your convenience, here is a list of common ways of wechat request server, with code and notes.

One: GET requests (most commonly used)

Wx. request({url: 'https://URL', // here is the path of your server API data: {}, // here is the parameter that can be filled in the server: method: 'GET', // declare the request // header: {}, // set the request header, GET request can not fill the success: Function (res){console.log(" return successful data :" + res.data) // return object, Console. log(" return successful data :"+ json.stringify (res.data)) // So you can see the background data}, fail: Function (fail) {complete: function(arr) {function(arr) {complete: function(arr) {}})Copy the code

POST request (I mainly use it when uploading data)

Basically similar to GET, note the two points in the comments.

Wx. request({url: '', method: 'post', data: {openid: }, header: {'content-type': 'Application /x-www-form-urlencoded' // We coded the content-type of the POST request in lower case, and we coded the content-type in lower case}, success: Function (res) {that.setData(test: res.data //test = data returned from server}); console.log(res.data) } });Copy the code

Third: form submission (this way is also more common, the method is also more diverse) code, form submission is very simple. 1. Submit the form using GET:

index.wxml

<form bindsubmit="formSubmit" bindreset="formReset"> <input type="text" class="input-text" name="username" Placeholder =" placeholder "/> <input type="text" class="input-text" name="password" placeholder=" placeholder" /> <button </button> </form>Copy the code

index.js

formSubmit: function (e) { var that = this; var formData = e.detail.value; Wx. request({url: ", data: formData, header: {' content-type ': 'application/json'}, success: function (res) { console.log(res.data) } }) },Copy the code

2. Use POST to submit the form. The code of index. WXML is the same as above

formSubmit: function (e) { var adds = e.detail.value; adds .openid = 11; wx.request({ url: '', data: adds, method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT header: {// set request header 'content-type': 'application/x-www-form-urlencoded'}, success: function (res) { console.log(JSON.stringify(res.data)) } fail: function (res) { console.log('cuowu' + ':' + res) } }) },Copy the code

** 4: Upload pictures and display them **

First paste the effect picture:



Again, it’s easy to go straight to the full code,

<form bindsubmit="formSubmit" id='2' bindreset="formReset"> <input style='display:none' name='program_id' Value ='{{program_id}}'></input> <view class='caigou_centent'> name="content" value='{{formdata}}' /> <view class="big-logos"> <image bindtap="upimg" src='.. /.. /images/jia.png'></image> <block wx:for="{{img_arr}}"> <view class='logoinfo'> <image src='{{item}}'></image> </view> </block> </view> <button class=' BTN 'formType="submit">Copy the code

js

var adds = {}; Page({ data: { img_arr: [], formdata: '', }, formSubmit: function (e) { var id = e.target.id adds = e.detail.value; adds.program_id = app.jtappid adds.openid = app._openid adds.zx_info_id = this.data.zx_info_id this.upload() }, upload: function () { var that = this for (var i=0; i < this.data.img_arr.length; i++) { wx.uploadFile({ url: 'https:***/submit', filePath: that.data.img_arr[i], name: 'content', formData: Adds, success: function (res) {console.log(res) if (res) {wx.showtoast ({title: 'Submitted for publishing! ', duration: 3000 }); } } }) } this.setData({ formdata: '' }) }, upimg: function () { var that = this; if (this.data.img_arr.length<3){ wx.chooseImage({ sizeType: ['original', 'compressed'], success: function (res) { that.setData({ img_arr: Tha.data.img_arr.concat (res.tempfilepaths)})}}else{wx.showtoast ({title: 'Max. 3 pictures ', icon:' Loading ', duration: 3000}); }},Copy the code

If console appears as shown below, the upload is successful