這篇文章主要介紹微信小程序中實(shí)現(xiàn)ajax請(qǐng)求服務(wù)器數(shù)據(jù)的示例,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)公司主營(yíng)善右網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都App定制開(kāi)發(fā),善右h5小程序開(kāi)發(fā)搭建,善右網(wǎng)站營(yíng)銷推廣歡迎善右等地區(qū)企業(yè)咨詢
頭部標(biāo)題和底部tab配置都在 app.json文件中,底部tab位最少兩個(gè),最多五個(gè)。下面是app.json文件代碼和相關(guān)注釋
{ "pages":[ "pages/index/index", "pages/tucao/tucao", "pages/center/center" ], "window":{ "backgroundTextStyle":"", "navigationBarBackgroundColor": "red", "navigationBarTitleText": "一個(gè)標(biāo)題而已", "navigationBarTextStyle":"white" }, "tabBar": { "list": [{ "pagePath": "pages/index/index", "text": "首頁(yè)", "iconPath": "/images/public/menu-cd.png", "selectedIconPath": "/images/public/menu.png" },{ "pagePath": "pages/tucao/tucao", "text": "吐槽", "iconPath": "/images/public/hot-cd.png", "selectedIconPath": "/images/public/hot.png" },{ "pagePath": "pages/center/center", "text": "我的", "iconPath": "/images/public/center-cd.png", "selectedIconPath": "/images/public/center.png" }], "borderStyle": "white" } }
這里我是通過(guò) 微信小程序wx.request
實(shí)現(xiàn)ajax請(qǐng)求服務(wù)器數(shù)據(jù)的,一個(gè)程序里面最多能有五個(gè)這樣的請(qǐng)求。下面是index.js的代碼
//index.js //獲取應(yīng)用實(shí)例 var app = getApp() Page({ data: { motto: 'Hello World', userInfo: {}, Industry:{} }, onLoad: function (res) { var that = this //調(diào)用應(yīng)用實(shí)例的方法獲取全局?jǐn)?shù)據(jù) app.getUserInfo(function(userInfo){ //更新數(shù)據(jù) that.setData({ userInfo:userInfo }) }) wx.request({ url: 'http://xx.xxxxx.com/xxx.php',//上線的話必須是https,沒(méi)有appId的本地請(qǐng)求貌似不受影響 data: {}, method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT // header: {}, // 設(shè)置請(qǐng)求的 header success: function(res){ console.log(res.data.result) that.setData({ Industry:res.data.result }) }, fail: function() { // fail }, complete: function() { // complete } }) } })
其中http://xx.xxxxx.com/xxx.php的返回?cái)?shù)據(jù)格式是一個(gè)json,格式如下
展示頁(yè)面就簡(jiǎn)單了,變量{{array}} 微信模版遍歷數(shù)據(jù)使用 wx:for
。index.wxml代碼如下:
{{userInfo.nickName}} {{index}}:{{item.name}}
以上是“微信小程序中實(shí)現(xiàn)ajax請(qǐng)求服務(wù)器數(shù)據(jù)的示例”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!