本篇文章小编给大家分享一下微信小程序加载数据库真实数据实现代码,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
微信小程序要加载网站数据库里面的真实数据,有一个硬性的要求,就是你的网站域名必须是https协议才行,要不然你第一步服务器域名配置你都通过不了。
一、进入小程序后台配置https服务器域名
二、程序中写好调用的数据,并返回json格式
//获取素材列表接口,该方法位于ApplicationHomeControllerWeixinController.class.php中 public function getdownList(){ $data=M('Material')->field('id,title,path,date,down,description,view')->order('date desc')->limit(6)->select(); echo json_encode($data); }
三、调用数据
因为我的下载模板是在index中,所有逻辑代码要写在index.js中,下面是具体的代码
/** * 生命周期函数--监听页面加载 */ onLoad: function () { console.log('onLoad') var that = this wx.request({ url: 'https://www.100txy.com/weixin/getdownlist', //真实的接口地址 data: {}, header: { 'content-type': 'application/json' }, success: function (res) { console.log(res.data) that.setData({ Industry: res.data //设置数据 }) }, fail: function (err) { console.log(err) } }) },
四、在列表模板渲染数据
进入到index.wxml中加载数据,具体代码如下
{{item.title}}n {{item.description}} 浏览 {{item.view}} 下载 {{item.down}}
微信小程序实现点击生成随机验证码示例代码
微信小程序如何创建投票活动 微信小程序创建投票活动方法
微信小程序如何实现列表的横向滑动方式 微信小程序实现列表的横向滑动方式代码
微信小程序如何实现watch监听 微信小程序实现watch监听代码示例
微信小程序利用for循环解决内容变更问题代码
微信小程序如何实现一个简单swiper 微信小程序实现一个简单swiper代码实例