【微信小程序】网络请求

环境:微信小程序开发工具

测试api(随机获取猫咪靓照):https://api.thecatapi.com/v1/images/search?limit=2

示例:

完整代码

request.wxml

html 复制代码
<button bind:tap="requestBtn" type="primary">网络请求</button>

<view class="box" wx:for="{{imgArr}}" wx:key="id">
<text>图片id:{{index+1}}</text>
<image src="{{item.url}}" mode="widthFix"/>
</view>
css 复制代码
/* pages/request/request.wxss */
.box{
  margin: 0 auto;
  text-align: center;
  border: 1px solid red;
  margin-bottom: 25rpx;
}
javascript 复制代码
// pages/request/request.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
imgArr:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  requestBtn(){
    wx.request({
      url: 'https://api.thecatapi.com/v1/images/search?limit=2',
      success:res=>{
        this.setData({
          imgArr:res.data
        })
      console.log(this.data.imgArr);
      }
    })
    
  },
相关推荐
HERR_QQ19 小时前
【unify】unify的微信小程序开发学习 (to be continued)
学习·微信小程序·小程序
racerun1 天前
小程序导航设置更多内容的实现方法
小程序
说私域1 天前
基于开源AI智能名片链动2+1模式S2B2C商城小程序的超级文化符号构建路径研究
人工智能·小程序·开源
mg6681 天前
微信小程序入门实例_____快速搭建一个快递查询小程序
微信小程序·小程序
程序员柳1 天前
基于微信小程序的校园二手交易平台、微信小程序校园二手商城源代码+数据库+使用说明,layui+微信小程序+Spring Boot
数据库·微信小程序·layui
Jyywww1211 天前
微信小程序学习笔记
笔记·学习·微信小程序
The_era_achievs_hero1 天前
微信小程序41~50
微信小程序·小程序
走,带你去玩2 天前
uniapp 微信小程序水印
微信小程序·小程序·uni-app
是一碗螺丝粉2 天前
🔥 微信H5视频自动播放终极秘籍:WeixinJSBridge竟是官方“通行证”?
微信小程序