收藏
回答

使用this.setData报‘of undefind’,怎么解?

const app = getApp()
Page({
    data: {
      msg:"1234567890"
    },
    onLoad() {
        wx.request({
          url : 'http://127.0.0.1:7001/logined',
          data : {
              usname : "werewrr",
              password : "12345678"
          },
          header: {
            'content-type': 'application/json' 
          },
          method : "POST",
          dataType : 'json',
          success(res){
              var d = res.data.data
              console.log(d)
              this.setData({
                msg: 'd[0].username'
              })
          }
        })
    }
  })


回答关注问题邀请回答
收藏

3 个回答

  • 游戏人生
    游戏人生
    2021-12-10

    this的作用域不对

    建议在wx.request 外面 声明 let self = this;

    self.setData()

    2021-12-10
    有用 2
    回复 2
    • HWH
      HWH
      发表于移动端
      2021-12-13
      噢,说到作用域就明白了怎么回事了😂
      2021-12-13
      回复
    • HWH
      HWH
      发表于移动端
      2021-12-13
      噢,说到作用域就明白了怎么回事了😂
      2021-12-13
      回复
  • Future
    Future
    2021-12-10

    在wx.request外 声明let _this = this _this.setData({})

    2021-12-10
    有用 2
    回复
  • brave
    brave
    2021-12-10
    success: (res) => {
       var d = res.data.data
      console.log(d)
      this.setData({
        msg: d[0].username
      })
    }
    
    2021-12-10
    有用 1
    回复
登录 后发表内容