无码日韩精品无码国产_一级做a爰片久久毛片潮喷_国产欧美国日产_久久9热re这里只有国产中文精品6_每天将为您更新成人影视在线看免费观看

Menu
小程序資訊
小程序資訊
微信小程序?qū)W習(xí)點(diǎn)滴《五》:網(wǎng)絡(luò)請(qǐng)求(POST請(qǐng)求)填坑指南
時(shí)間:2016-10-18 18:09:00
微信小程序開(kāi)發(fā)中網(wǎng)絡(luò)請(qǐng)求必不可少.GET.POST請(qǐng)求是最常用的.GET請(qǐng)求
POST請(qǐng)求的時(shí)候有好幾個(gè)坑.我已經(jīng)為大家填好了.

按照文檔,肯定是這么寫(xiě).那就入坑了.
1. 'Content-Type': 'application/json'用在get請(qǐng)求中沒(méi)問(wèn)題.
POST請(qǐng)求就不好使了.需要改成: "Content-Type": "application/x-www-form-urlencoded"

2. 加上method: "POST"
3.data: { cityname: "上海", key: "1430ec127e097e1113259c5e1be1ba70" }寫(xiě)成json格式這樣也是請(qǐng)求不到數(shù)據(jù)的.需要轉(zhuǎn)格式.
下面直接貼代碼:
 3.1
  1. <span style="font-size:24px;">//index.js
  2. //獲取應(yīng)用實(shí)例
  3. var app = getApp()
  4. Page( {
  5. data: {
  6. toastHidden: true,
  7. city_name: '',
  8. },
  9. onLoad: function() {
  10. that = this;
  11. wx.request( {
  12. url: "http://op.juhe.cn/onebox/weather/query",
  13. header: {
  14. "Content-Type": "application/x-www-form-urlencoded"
  15. },
  16. method: "POST",
  17. //data: { cityname: "上海", key: "1430ec127e097e1113259c5e1be1ba70" },
  18. data: Util.json2Form( { cityname: "上海", key: "1430ec127e097e1113259c5e1be1ba70" }),
  19. complete: function( res ) {
  20. that.setData( {
  21. toastHidden: false,
  22. toastText: res.data.reason,
  23. city_name: res.data.result.data.realtime.city_name,
  24. date: res.data.result.data.realtime.date,
  25. info: res.data.result.data.realtime.weather.info,
  26. });
  27. if( res == null || res.data == null ) {
  28. console.error( '網(wǎng)絡(luò)請(qǐng)求失敗' );
  29. return;
  30. }
  31. }
  32. })
  33. },
  34. onToastChanged: function() {
  35. that.setData( { toastHidden: true });
  36. }
  37. })
  38. var that;
  39. var Util = require( '../../utils/util.js' );</span>
3.2
  1. <span style="font-size:24px;"><!--index.wxml-->
  2. <view class="container">
  3. <toast hidden="{{toastHidden}}" bindchange="onToastChanged">
  4. {{toastText}}
  5. </toast>
  6. <view>{{city_name}}</view>
  7. <view>{{date}}</view>
  8. <view>{{info}}</view>
  9. </view></span>
3.3
  1. <span style="font-size:24px;">//util.js
  2. function json2Form(json) {
  3. var str = [];
  4. for(var p in json){
  5. str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));
  6. }
  7. return str.join("&");
  8. }
  9. module.exports = {
  10. json2Form:json2Form,
  11. }</span>

        

希望對(duì)大家有幫助

咨詢(xún)
微信掃碼咨詢(xún)
電話(huà)咨詢(xún)
400-888-9358