这篇文章给大家介绍的内容是关于微信小程序中request请求封装的代码分析,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
request
只用POST,只封装了POST,等有用到GET再重写,接口用的ThinkPHP5.0
主要代码
var apiurl = "xxxxx";
function http_post(controller,data,cb){
wx.request({
url:apiurl+controller,
data:data,
method:'post',
header:{'Content-Type':'application/x-www-form-urlencoded'},
success:function(res){
return typeof cb == "function" && cb(res.data)
},
fail:function(res){
return typeof cb == "function" && cb(false)
}
})
}
module.exports = {
http_post:http_post,//post请求
}前端js使用
var wxq = require('../../utils/wxrequest.js');
var openid = wx.getStorageSync('openid');//获取缓存里面的openid
wxq.http_post('这里是控制器/方法',{'openid':openid},function(res){
console.info('回调',res)
})相关推荐:
微信是一款手机通信软件,支持通过手机网络发送语音短信、视频、图片和文字。微信可以单聊及群聊,还能根据地理位置找到附近的人,带给大家全新的移动沟通体验,有需要的小伙伴快来保存下载体验吧!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号