小程序中post請(qǐng)求的示例

這篇文章將為大家詳細(xì)講解有關(guān)小程序中post請(qǐng)求的示例,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

“只有客戶發(fā)展了,才有我們的生存與發(fā)展!”這是創(chuàng)新互聯(lián)建站的服務(wù)宗旨!把網(wǎng)站當(dāng)作互聯(lián)網(wǎng)產(chǎn)品,產(chǎn)品思維更注重全局思維、需求分析和迭代思維,在網(wǎng)站建設(shè)中就是為了建設(shè)一個(gè)不僅審美在線,而且實(shí)用性極高的網(wǎng)站。創(chuàng)新互聯(lián)對(duì)網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、網(wǎng)站制作、網(wǎng)站開發(fā)、網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站優(yōu)化、網(wǎng)絡(luò)推廣、探索永無止境。

小程序中post請(qǐng)求的示例

按照文檔,肯定是這么寫.那就入坑了.

1. 'Content-Type': 'application/json'用在get請(qǐng)求中沒問題.

POST請(qǐng)求就不好使了.需要改成: "Content-Type": "application/x-www-form-urlencoded"


2. 加上method: "POST"

3.data: { cityname: "上海", key: "1430ec127e097e1113259c5e1be1ba70" }寫成json格式這樣也是請(qǐng)求不到數(shù)據(jù)的.需要轉(zhuǎn)格式.

下面直接貼代碼:

3.1

<span style="font-size:24px;">//index.js  
//獲取應(yīng)用實(shí)例  
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: "上海", key: "1430ec127e097e1113259c5e1be1ba70" },  
      data: Util.json2Form( { cityname: "上海", key: "1430ec127e097e1113259c5e1be1ba70" }),  
      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( '網(wǎng)絡(luò)請(qǐng)求失敗' );  
          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>

小程序中post請(qǐng)求的示例

關(guān)于“小程序中post請(qǐng)求的示例”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

網(wǎng)站名稱:小程序中post請(qǐng)求的示例
網(wǎng)頁(yè)地址:http://bm7419.com/article18/jjscdp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、企業(yè)建站Google、網(wǎng)站改版營(yíng)銷型網(wǎng)站建設(shè)、建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)