微信小程序 POST请求的实例详解
2019/6/27 20:38:21
本文主要是介绍微信小程序 POST请求的实例详解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
微信小程序 POST请求的实例详解
在微信小程序里post请求和get情求,写法差不多,但是还是有一点点不同的,下面利用post请求做一个查询天气的微信小程序demo。
页面代码:
<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: { //请求头和ajax写法一样 "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( '网络请求失败' ); return; } setTimeout(function(){ wx.hideToast() },2000) } }) } })
依赖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, }
展示图:
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
这篇关于微信小程序 POST请求的实例详解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-06vue 新建功能多条数据(还没和后端交互)还能看详情 数据是前端缓存到本地吗?-icode9专业技术文章分享
- 2024-05-30React Native常用组件-点击组件
- 2024-05-30uniapp+vue3+uv-ui手机端后台OA管理模板
- 2024-05-29Python网络爬虫的时候json=就是让你少写个json.dumps()
- 2024-05-27React Native常用组件-展示组件
- 2024-05-27React Native常用组件-列表组件
- 2024-05-09vue3开发前端表单缓存自定义指令,移动端h5必备插件
- 2024-05-09React Hooks在class组件中的使用方式
- 2024-03-30[OIDC in Action] 2. 基于OIDC(OpenID Connect)的SSO(纯JS客户端)
- 2024-03-29terraform jsonencode