微信小程序请求API

js部分代码

 1 Page({
 2 
 3   /**
 4    * 页面的初始数据
 5    */
 6   data: {
 7     //声明一个空数组
 8     array:[],
 9   },
10   onclick:function(e){
11     var that=this; 
12     wx.request({
13       url: 'http://localhost:64395/api/Books/BookShow',
14       data:{
15       },
16       success:function(res){
17         //变量赋值
18         that.setData({
19           array:res.data
20         })
21       }
22     })
23   },
24   /**
25    * 生命周期函数--监听页面加载
26    */
27   onLoad: function (options) {
28    
29 
30   },

wxml部分代码

1 <view class="btn-area" id="buttonContainer">
2   <button type="primary" plain="true" bindtap="onclick">请求API数据</button>
3 </view>
4 <view class="page__desc" wx:for="{{array}}">
5                 <view>{{item.ReaName}}</view>
6 </view>

效果图

 

posted @ 2021-07-28 14:16  海不会等风来  阅读(110)  评论(0)    收藏  举报