微信小程序wx.request请求后success回调的数据无法显示到页面上

Posted 征途黯然.

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信小程序wx.request请求后success回调的数据无法显示到页面上相关的知识,希望对你有一定的参考价值。

出错原因

  我在success回调函数是这样赋值的:

var that = this;
wx.request(
  url: this.data.host + "/system/dept/list",
  method : "GET",
  success: function (res) 
    if(res.data.code == 200)
        that.data.deptInfo:res.data.data;
    
  
)

  微信官方说明:

  Page.prototype.setData():setData函数用于将数据从逻辑层发送到视图层,同时改变对应的 this.data 的值。注意: 直接修改 this.data 无效,无法改变页面的状态,还会造成数据不一致。 单次设置的数据不能超过1024kB,请尽量避免一次设置过多的数据。

解决办法

  应该这样赋值:

var that = this;
wx.request(
  url: this.data.host + "/system/dept/list",
  method : "GET",
  success: function (res) 
    if(res.data.code == 200)
      that.setData(
        deptInfo:res.data.data
      );
    
  
)

以上是关于微信小程序wx.request请求后success回调的数据无法显示到页面上的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序的wx.request请求方法,标准写法

微信小程序 请求超时处理

微信小程序 在使用wx.request时显示加载中

微信小程序wx.request的回调使用

微信小程序获取openid

微信小程序 怎么将js页面wx.request请求的数据渲染到wxml页面中去