注:本 demo 使用的豆瓣的API,如果无效请使用其他API
本文实例讲述了微信小程序使用wx.request请求服务器json数据并渲染到页面操作。分享给大家供大家参考,具体如下:
index.wxml
<view><button type='primary' bindtap='send'>请求数据</button></view>
<view wx:for="{{list}}" wx:key="list">
<view><image src='{{item.image}}'></image></view>
</view>
index.js
Page({
data: {
},
//请求数据
send: function () {
var that = this
wx.request({
url: 'https://douban.uieee.com/v2/movie/subject/1764796',
header: {
'Content-Type': 'application/xml', //这种方式可以
// 'Content-Type': 'json' //这种方式也可以
},
success: function (res) {
// console.log(res.data)
console.log(res.data)
that.setData({
list: res.data.photos,
//res代表success函数的事件对,data是固定的,list是数组
})
}
})
},
})