欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序 POST請(qǐng)求的實(shí)例詳解

 更新時(shí)間:2017年09月29日 11:52:12   作者:king-w  
這篇文章主要介紹了 微信小程序 POST請(qǐng)求的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下

 微信小程序 POST請(qǐng)求的實(shí)例詳解

在微信小程序里post請(qǐng)求和get情求,寫(xiě)法差不多,但是還是有一點(diǎn)點(diǎn)不同的,下面利用post請(qǐng)求做一個(gè)查詢(xún)天氣的微信小程序demo。

頁(yè)面代碼:

<view> {{title}} </view>

<span style="font-size:24px;"><!--index.wxml--> 
<view class="container"> 
  <view style="color:{{red}}">{{city_name}}</view> 
  <view>{{date}}</view> 
  <view>{{info}}</view> 
</view></span>

js代碼:

var app = getApp();
var that; 
var Util = require( '../../utils/util.js' );
Page( { 
 data: {  
  city_name: '', 
  title:'',
  red:'green'
 }, 
 onLoad: function(options) { 
   this.setData({
     title:options.title
   })
  that = this; 
  wx.request( { 
   url: "http://op.juhe.cn/onebox/weather/query", 
   header: { 
   //請(qǐng)求頭和ajax寫(xiě)法一樣
    "Content-Type": "application/x-www-form-urlencoded" 
   }, 
   method: "POST",  
   data: Util.json2Form( { cityname: "北京", key: "1430ec127e097e1113259c5e1be1ba70" }), 
   complete: function( res ) { 

     wx.showToast({
      title:'成功',
      icon:'success',
      duration:2000
     })

    that.setData( {  
      red:'red',
     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; 
    } 

     setTimeout(function(){
       wx.hideToast()
     },2000)
   } 
  }) 
 }

}) 

依賴(lài)util.js代碼:

function json2Form(json) { 
  var str = []; 
  for(var p in json){ 
    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p])); 
  } 
  return str.join("&"); 
} 

module.exports = {
 formatTime: formatTime,
 json2Form:json2Form,
}

展示圖:

如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論