微信小程序如何加載數(shù)據(jù)庫真實數(shù)據(jù)的實現(xiàn)
微信小程序要加載網(wǎng)站數(shù)據(jù)庫里面的真實數(shù)據(jù),有一個硬性的要求,就是你的網(wǎng)站域名必須是https協(xié)議才行,要不然你第一步服務(wù)器域名配置你都通過不了,小編我也是前不久申請的https://www.100txy.com,具體申請步驟大家自行去申請吧,這里我就不做過多的介紹。下面我就以加載我博客素材最新的6條數(shù)據(jù)為案例來分析,下面是詳細(xì)步驟。
一、進(jìn)入小程序后臺配置https服務(wù)器域名
二、程序中寫好調(diào)用的數(shù)據(jù),并返回json格式
//獲取素材列表接口,該方法位于Application\Home\Controller\WeixinController.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); }
三、調(diào)用數(shù)據(jù)
因為我的下載模板是在index中,所有邏輯代碼要寫在index.js中,下面是具體的代碼
/** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ 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 //設(shè)置數(shù)據(jù) }) }, fail: function (err) { console.log(err) } }) },
四、在列表模板渲染數(shù)據(jù)
進(jìn)入到index.wxml中加載數(shù)據(jù),具體代碼如下
<view class="newsInfo"> <block wx:for="{{Industry}}" > <view class="newsList" wx:for-index="idx" bindtap="showDetail" id="{{item.id}}"> <view class="pic"> <image style="width:110px;height:80px;" src="https://www.100txy.com/{{item.path}}"></image> </view> <view class="news_title"> <text class="title_subject">{{item.title}}\n</text> <text class="title">{{item.description}}</text><text class="dianping">瀏覽 {{item.view}} 下載 {{item.down}}</text> </view> </view> <view class="hr"></view> </block> </view>
最后效果如下:這就是我博客素材最新的6條數(shù)據(jù),該小程序源碼我已經(jīng)放到了github上了,需要的朋友可以去下載看看。
到此這篇關(guān)于微信小程序如何加載數(shù)據(jù)庫真實數(shù)據(jù)的實現(xiàn)的文章就介紹到這了,更多相關(guān)小程序加載數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript數(shù)據(jù)類型中的一些小知識點(推薦)
這篇文章主要介紹了javascript數(shù)據(jù)類型中的一些小知識點,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04