zoukankan      html  css  js  c++  java
  • 小程序,wx.request;动态向服务器端请求数据。

     

     

    wx.request 通过微信封装的ajax,动态向服务器端请求数据。以下是在练习微信小程序时写的测试代码。

    其中index.js文件如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    //测试:
    wx.request({
         url:'http://hd.jz-lm.com/mobile/Ceshi/index',
         method: 'GET',
         data: {},
         header: {
             'Accept''application/json'
         },
         success: function(res) {
             console.log('-----------')
             console.log(res);
             console.log(res.data);
             console.log('-----------')
             that.setData({
               tests: res.data
             });
          
         }
     })

    通过console.log(res)打印出来的数据如下

    通过console.log(res.data);打印出来的是一个二维数组信息,如下。,

    index.wxml文件如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <!-- 测试信息 -->
    <view>
        <swiper class="swiper_box" indicator-dots="{{indicatorDots}}" vertical="{{vertical}}"
                autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}" bindchange="swiperchange">
            <block wx:for="{{tests}}">
                <swiper-item>
                    <image src="{{'http://hd.jz-lm.com'+item.original_img}}" class="slide-image"/>
                </swiper-item>
            </block>
        </swiper>
    </view>

     整个流程是请求URL地址,获得json数据,将得到的json数据,找到需要的数据赋值给变量,写入data中。以供前台使用。

    微信wxml文件通过 block for函数循环,输出数组变量信息。目前做的是简单的向服务器端请求数据,并未向服务器端传入data数据,实现动态双向数据交互。

  • 相关阅读:
    微信视频号里的视频如何保存下来呢?
    jQuery实现平面图区域标记
    npm安装教程 yarn 基本安装和使用
    VitePress :VuePress 下一代建站工具
    基于CentOS的ECS实例实现OSS反向代理
    备份MySQL数据库到七牛云的shell脚本
    ShedLock 解决分布式结构下定时任务重复执行问题
    linux清除日志和文件缓存
    Communications link failure:The last packet successfully received from the server was 0 millisecond ago
    CentOS7安装nodeJs
  • 原文地址:https://www.cnblogs.com/xiongdahei/p/7071239.html
Copyright © 2011-2022 走看看