小程式學習記錄:讀取discuz生成的json資料
阿新 • • 發佈:2018-11-12
在discuz後臺資料庫中建表,testjson,並輸入兩條記錄:
testjson.php:
<?php if(!defined('IN_DISCUZ')) { exit('Access Denied'); } header("Content-Type:text/html;charset=GB2312"); require_once libfile('function/discuzcode'); $query = DB::query('SELECT * FROM TESTJSON'); while($data = DB::fetch($query)){ $list[] = array( 'id' => urlencode($data['Id']), 'txt' => urlencode($data['txt']) ); } echo urldecode(json_encode($list)); ?>
修改小程式自動生成的模板檔案index.js檔案中的bindViewTap事件,原來單擊頭像顯示操作日誌,變成console顯示json內容:
bindViewTap: function() { // wx.navigateTo({ // url: '../logs/logs' // }) wx.request({ method: "GET", url: 'http://www.XXXXX.com/test_ent.php', data: {}, header: { 'content-type': 'application/json' }, success: function(res) { console.log(res) } }) },
設定一下專案域名不限制:詳情——複選“不校驗合法域名...”
單擊頭像,console顯示結果: