html52.com
我爱小程序-开发者交流社区

setData 数据格式一直不被swiper解析 求大神

问题模块
API和组件

js文件:

onLoad: function () {

    var that = this;

    wx.request({

      url: 'https:/xxxxx',//上线的话必须是https,没有appId的本地请求貌似不受影响

      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT  

      // header: {}, // 设置请求的 header  

      dataType:'json',

      success: function (res) {

        console.log(res.data);

        that.setData({

          sz:res.data

        })

       

      },

    });


请求到的数据:[{"image":"\/static\/index\/picture\/banner1.jpg"},{"image":"\/static\/index\/picture\/banner2.jpg"},{"image":"\/static\/index\/picture\/banner3.jpg"}]

xml文件:

<swiper class="swiper" indicator-dots="true" autoplay="true" interval="5000" duration="1000">  

   <block wx:for="{{sz}}" wx:key='key'> 

    <swiper-item>  

     <image src="https:/xxxx/{{item.image}}" class="slide-image" mode="aspectFill"/>  

    </swiper-item>  

   </block>  

  </swiper> 


网友回复

曾华经:

沖沖 所说res.data是一个字符串,必须先解析JSON.parse(res.data)再setData,wxml里拼接字符串改下

  <image src="{{“https://xxxx”+item.image}}" class="slide-image" mode="aspectFill"/>  


MuZe!:

改为JSON.parse(res.data)   后报错啊

MuZe!:

MuZe!:数据没有取到,但是循环了好多次,好像是死循环

数据没有取到,但是循环了好多次,好像是死循环

曾华经:

setData后你可以调试模式看下wxml中实际的src字符串是如何的?

沖沖:

res.data是个json字符串吧,改为JSON.parse(res.data)

MuZe!:

MuZe!:这个是xml中{{sz}}的结果

这个是xml中{{sz}}的结果

MuZe!:

请求过来的原数据直接复制到xml中可以正常显示,但是setData之后,数据就直接输出到模板了

曾华经:

图片地址的字符串拼接不对,还多个“/”

图片地址的字符串拼接不对,还多个“/”

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

真正的个人免签约支付接口

云免签H5支付