微信小程序之POST请求
2016-11-17 17:45
260 查看
在微信小程序里post请求和get情求,写法差不多,但是还是有一点点不同的,下面利用post请求做一个查询天气的微信小程序demo。
页面代码:
js代码:
依赖util.js代码:
展示图:
页面代码:
<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写法一样 4000 "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请求
- 微信小程序post请求服务器端无法获取的解答
- 微信小程序wx.request请求用POST后台得不到传递数据
- 微信小程序 POST请求(网络请求)详解及实例代码
- 微信小程序------MD5加密(支持中文和不支持中文)和网络请求(get和post)
- 微信小程序开发之大坑记之post请求
- 微信小程序开发之网络请求(POST请求)
- 【微信小程序】request请求POST提交数据,记得要加上header
- 微信小程序入坑那些事情-POst 请求
- 微信小程序 post git 的请求坑
- 微信小程序 网络请求(post请求,get请求)
- 微信小程序开发之网络请求(POST请求)
- 微信小程序开发之大坑记之post请求
- 微信小程序:wx.request之post请求后端无法获取数据的问题
- 微信小程序 POST请求(网络请求)详解及实例代码
- 微信小程序之网络请求(post)
- 微信小程序POST请求参数传递不到后台解决方案
- 微信小程序 post请求服务器接收不到参数
- 微信小程序------MD5加密(支持中文和不支持中文)和网络请求(get和post)
- 微信小程序开发之网络请求(POST请求)