微信小程序踩坑記(持續更新)

1、POST請求獲取到請求參數爲null

官方文檔的例子是:
wx.request({
  url: 'test.php', //僅爲示例,並非真實的接口地址
  data: {
     x: '' ,
     y: ''
  },
  header: {
      'content-type': 'application/json' // 默認值
  },
  success: function(res) {
    console.log(res.data)
  }
})
如果請求是POST請求如果沒有
header: { "Content-Type": "application/x-www-form-urlencoded" },那麼後臺獲取不到請求參數。

2、button樣式border:none設置無效


去除button邊框樣式需要在::after僞類選擇器中設置,如下:
.handBt{
background: #fff;
font-size: 14px;
height: auto;
padding: 0px;
color: #0AB944;
}
.handBt::after{
border:none;
}

3、request請求的success回調中獲取data數據,報undefined錯誤

微信小程序中,在wx.request({});方法調用成功或者失敗之後,有時候會需要獲取頁面初始化數據data的情況,如果使用this.data來獲取,會報undefiend。原因是this表示當前對象,在回調函數中,this對象已經改變,所以獲取不到data中的數據,解決方式是在請求之前,調用var that=this;

在回調函數中使用that.data.**就能獲取到數據了。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章