本文實例講述了微信小程序開發(fā)wx.request實現(xiàn)后臺數(shù)據(jù)交互功能。分享給大家供大家參考,具體如下:
記錄微信小程序wx.request這個api在跟后臺交互時遇上的問題。
1、根據(jù)資料,完成第一步,請求發(fā)送,代碼如下:
wx.request({ url: 'https://localhost:8443/xiaochengxu/addBill.do', data: e.detail.value, method: 'POST', success:function(res) { console.log('submit success'); }, fail:function(res){ console.log('submit fail'); }, complete:function(res){ console.log('submit complete'); }}) 后臺成功接收到請求,控制臺也打印了submit success和submit complete,但是,后臺請求并未接收到數(shù)據(jù),打開調(diào)試,發(fā)現(xiàn)數(shù)據(jù)都在request payload中,所以后臺無論是springmvc的映射bean還是req.getParameter都拿不到參數(shù)。
解決方法參考本站:http://m.survivalescaperooms.com/kaifa/java/289893.html
簡單說就是增加了header: {'content-type': 'application/x-www-form-urlencoded'},后臺成功獲取數(shù)據(jù)。
至此,代碼如下:
wx.request({ url: 'https://localhost:8443/xiaochengxu/addBill.do', data: e.detail.value, method: 'POST', header: {'content-type': 'application/x-www-form-urlencoded'}, success:function(res) { console.log('submit success'); }, fail:function(res){ console.log('submit fail'); }, complete:function(res){ console.log('submit complete'); }})2、接收請求返回數(shù)據(jù)
這一步問題不大,我是按照json格式返回的,只是按照官網(wǎng)寫的console.log(res.data)的話,會在控制臺打印Object,帶上參數(shù)名就好了,比如res.data.code
希望本文所述對大家微信小程序開發(fā)有所幫助。
新聞熱點
疑難解答