微信小程序wx.request實(shí)現(xiàn)后臺(tái)數(shù)據(jù)交互功能分析
本文實(shí)例講述了微信小程序wx.request實(shí)現(xiàn)后臺(tái)數(shù)據(jù)交互功能。分享給大家供大家參考,具體如下:
記錄微信小程序wx.request這個(gè)api在跟后臺(tái)交互時(shí)遇上的問題。
1、根據(jù)資料,完成第一步,請(qǐng)求發(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');
}
})
后臺(tái)成功接收到請(qǐng)求,控制臺(tái)也打印了submit success和submit complete,但是,后臺(tái)請(qǐng)求并未接收到數(shù)據(jù),打開調(diào)試,發(fā)現(xiàn)數(shù)據(jù)都在request payload中,所以后臺(tái)無論是springmvc的映射bean還是req.getParameter都拿不到參數(shù)。
解決方法參考本站:http://chabaoo.cn/article/129039.htm
簡(jiǎn)單說就是增加了header: {'content-type': 'application/x-www-form-urlencoded'},后臺(tái)成功獲取數(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、接收請(qǐng)求返回?cái)?shù)據(jù)
這一步問題不大,我是按照json格式返回的,只是按照官網(wǎng)寫的console.log(res.data)的話,會(huì)在控制臺(tái)打印Object,帶上參數(shù)名就好了,比如res.data.code
希望本文所述對(duì)大家微信小程序開發(fā)有所幫助。
- 微信小程序wx.request攔截器使用詳解
- 微信小程序設(shè)置全局請(qǐng)求URL及封裝wx.request請(qǐng)求操作示例
- 微信小程序使用wx.request請(qǐng)求服務(wù)器json數(shù)據(jù)并渲染到頁面操作示例
- 微信小程序使用request網(wǎng)絡(luò)請(qǐng)求操作實(shí)例
- 微信小程序網(wǎng)絡(luò)請(qǐng)求wx.request詳解及實(shí)例
- 微信小程序 wx.request(接口調(diào)用方式)詳解及實(shí)例
- 微信小程序 wx.request合法域名配置詳解
- 微信小程序request請(qǐng)求封裝,驗(yàn)簽代碼實(shí)例
相關(guān)文章
淺析JavaScript作用域鏈、執(zhí)行上下文與閉包
javascript parseInt與Number函數(shù)的區(qū)別
抽出www.templatemonster.com的鼠標(biāo)懸停加載大圖模板的代碼
Bootstrap基本插件學(xué)習(xí)筆記之按鈕(21)
JS數(shù)組去重常用方法實(shí)例小結(jié)【4種方法】

