昨天下载了一个微信小程序的开发者工具,大概看了一下文档,简单的用他的方法实现了ajax请求。本文主要介绍了微信小程序ajax实现请求服务器数据及模版遍历数据功能,结合实例形式分析了微信小程序ajax调用及模板wx:for循环列表渲染相关操作技巧,需要的朋友可以参考下,希望能帮助到大家。

头部标题和底部tab配置都在 app.json文件中,底部tab位最少两个,最多五个。下面是app.json文件代码和相关注释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | {
"pages" :[
"pages/index/index" ,
"pages/tucao/tucao" ,
"pages/center/center"
],
"window" :{
"backgroundTextStyle" : "" ,
"navigationBarBackgroundColor" : "red" ,
"navigationBarTitleText" : "一个标题而已" ,
"navigationBarTextStyle" : "white"
},
"tabBar" : {
"list" : [{
"pagePath" : "pages/index/index" ,
"text" : "首页" ,
"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"
}
}
|
这里我是通过 微信小程序wx.request实现ajax请求服务器数据的,一个程序里面最多能有五个这样的请求。下面是index.js的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | var app = getApp()
Page({
data: {
motto: & #39;Hello World',
userInfo: {},
Industry:{}
},
onLoad: function (res) {
var that = this
app.getUserInfo( function (userInfo){
that.setData({
userInfo:userInfo
})
})
wx.request({
url: & #39;http://xx.xxxxx.com/xxx.php',//上线的话必须是https,没有appId的本地请求貌似不受影响
data: {},
method: & #39;GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
success: function (res){
console.log(res.data.result)
that.setData({
Industry:res.data.result
})
},
fail: function () {
},
complete: function () {
}
})
}
})
|
其中http://xx.xxxxx.com/xxx.php的返回数据格式是一个json,格式如下

展示页面就简单了,变量{{array}} 微信模版遍历数据使用 wx:for 。index.wxml代码如下:
1 2 3 4 5 6 7 8 9 10 | <!--index.wxml-->
<view class = "container" >
<view bindtap= "bindViewTap" class = "userinfo" >
<image class = "userinfo-avatar" src= "{{userInfo.avatarUrl}}" background-size= "cover" ></image>
<text class = "userinfo-nickname" >{{userInfo.nickName}}</text>
</view>
</view>
<view wx: for = "{{Industry}}" wx:ket= "{{index}}" >
{{index}}:{{item.name}}
</view>
|
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。