小程序中post请求的示例(post,小程序,移动开发)

时间:2024-04-19 14:27:11 作者 : 石家庄SEO 分类 : 移动开发
  • TAG :

小程序中post请求的示例

按照文档,肯定是这么写.那就入坑了.

1. 'Content-Type': 'application/json'用在get请求中没问题.

POST请求就不好使了.需要改成: "Content-Type": "application/x-www-form-urlencoded"


2. 加上method: "POST"

3.data: { cityname: "上海", key: "1430ec127e097e1113259c5e1be1ba70" }写成json格式这样也是请求不到数据的.需要转格式.

下面直接贴代码:

3.1

<spanstyle="font-size:24px;">//index.js//获取应用实例varapp=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('网络请求失败');return;}}})},onToastChanged:function(){that.setData({toastHidden:true});}})varthat;varUtil=require('../../utils/util.js');</span>

3.2

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

3.3

<spanstyle="font-size:24px;">//util.jsfunctionjson2Form(json){varstr=[];for(varpinjson){str.push(encodeURIComponent(p)+"="+encodeURIComponent(json[p]));}returnstr.join("&");}module.exports={json2Form:json2Form,}</span>

小程序中post请求的示例

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:小程序中post请求的示例的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:PostgreSQL备份和恢复自动化的方法下一篇:

11 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18