I have been learning small programs recently, and I just learned wx.request. I used php mysql to output a json data format, link: http://www.likeyunba.com/tmp/...
I want to render this data on the mini program page.
index.wxml
<!--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">{{post.id}}</text>
</view>
<view class="usermotto">
<text class="user-motto">{{motto}}</text>
<text class="user-motto">{{post.title}}</text>
<image class="userinfo-avatar" src="{{post.imgurl}}" background-size="cover"></image>
<text class="user-motto">{{post.url}}</text>
<text class="user-motto">{{post.int}}</text>
</view>
</view>
index.js
//index.js
//获取应用实例
var app = getApp()
Page({
data: {
userInfo: {},
post:[]
},
//事件处理函数
bindViewTap: function() {
var that = this;
wx.request({
url: 'http://www.likeyunba.com/tmp/json.php',
method:'GET',
success: function(res){
console.log(res);
that.setData({
post:res.data[0]
})
},
fail: function(res) {
console.log("--fail--")
}
})
},
onLoad: function () {
console.log('onLoad')
var that = this
//调用应用实例的方法获取全局数据
app.getUserInfo(function(userInfo){
//更新数据
that.setData({
userInfo:userInfo
})
})
}
})
When the page is refreshed, my WeChat avatar will be displayed. Clicking on the WeChat avatar will render the json format data output by PHP.
But my http://www.likeyunba.com/tmp/... has two pieces of data, but the page only renders one piece. Is there any way to render all the data? And when I insert new data into the database, the mini program page also renders the latest data.
There are two pieces of json data. You must use wx:for. Directly assign res.data to post instead of data[0], so there must be only one piece.
You can take a look at the official documentation on how to use wx:for.
Otherwise, take a look at a small program demo I made: https://github.com/lin-xin/wx...
Familiarize yourself with this example, and your small program will basically be very good