WeChat mini-program POST request (Network request) details and instance code, mini-program post

Source: Internet
Author: User

Small Program POST request (Network request) details and instance code, small program post

Applet POST request

Network requests are essential in applet development. GET. POST requests are the most common. GET requests. There are several pitfalls in POST requests. I have already filled them out for you.

 

According to the document, it must be written in this way.

1. 'content-type': 'application/json' is used in get requests.

The post request is not easy to use. You need to change it to: "Content-Type": "application/x-www-form-urlencoded"

2016.11.10 update: a post request is successful after the content-type is changed to lowercase.

2. Add method: "POST"

3. data: {cityname: "Shanghai", key: "1430ec1_e097e1113259c5e1be1ba70"} is written in json format, which means no data is requested. The format must be converted.

The following code is directly pasted:

3.1

<Span style = "font-size: 24px;"> // index. js // obtain the application instance var app = getApp () Page ({data: {toastHidden: true, city_name: '',}, onLoad: function () {that = this; wx. request ({url: "http://op.juhe.cn/onebox/weather/query", header: {"Content-Type": "application/x-www-form-urlencoded"}, method: "POST ", // data: {cityname: "Shanghai", key: "1430ec1_e097e1113259c5e1be1ba70"}, data: Util. json2Form ({cityname: "Shanghai", key: "1430ec1_e097e1113259c5e1be1ba70"}), complete: function (res) {that. setData ({toastHidden: false, toastText: res. data. reason, city_name: res. data. result. data. realtime. city_name, date: res. data. result. data. realtime. date, info: res.data.result.data.realtime.weather.info,}); if (res = null | res. data = null) {console. error ('network request failed'); return ;}})}, onToastChanged: function () {that. setData ({toastHidden: true}) ;}) var that; var Util = require ('.. /.. /utils/util. js'); </span>

3.2

<span style="font-size:24px;"><!--index.wxml--> <view class="container">   <toast hidden="{{toastHidden}}" bindchange="onToastChanged">     {{toastText}}   </toast>   <view>{{city_name}}</view>   <view>{{date}}</view>   <view>{{info}}</view> </view></span> 

3.3

<span style="font-size:24px;">//util.js function json2Form(json) {   var str = [];   for(var p in json){     str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));   }   return str.join("&"); } module.exports = {  json2Form:json2Form, }</span> 


I hope to help you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.