網(wǎng)站首頁 編程語言 正文
????????微信小程序開發(fā)中網(wǎng)絡(luò)請求必不可少.GET .POST請求是最常用的.GET請求,POST請求的時候有好幾個坑.我已經(jīng)為大家填好了.
1.? 'Content-Type': 'application/json'用在get請求中沒問題.
????????之前的.GET 請求都是如下這樣寫:(給大家舉個例子)
wx.request({
url: url,
data: {
teacherid: teacherid
},
header: {
'content-type': 'application/json'
},
success: (res) => {
// console.log(res.data);
this.setData({
testpaper: res.data.testpaper,
teacher : res.data.teacher
});
}
})
但是post方式提交的話就有所改變了,給大家列出以下幾點注意事項:1.? 'Content-Type': 'application/json'用在get請求中沒問題.
POST請求就不好使了.需要改成 : ?"Content-Type": "application/x-www-form-urlencoded"
2. 加上 method: "POST"
3.?data: { answer : {?"a":10,"b":8,"c":6 } } 寫成json格式這樣也是請求不到數(shù)據(jù)的.需要轉(zhuǎn)格式.
這里我用JSON.Stringify() 將json對象轉(zhuǎn)換成json字符串格式
部分代碼分享給大家,這里answer與student 都是json對象格式需要轉(zhuǎn)換
wx.request({
url : "https://www.",
method: "POST",
data: {
answer : JSON.stringify(this.data.answer),
score : _score,
pjid : this.data.pj.pjid,
testpaperid : this.data.pj.testpaperid,
student : JSON.stringify(this.data.student),
message : this.data.message
},
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
success: function (res) {
console.log(res.data);
wx.navigateBack({
delta: 1 //小程序關(guān)閉當前頁面返回上一頁面
})
wx.showToast({
title: '評教成功!',
icon: 'success',
duration: 2000
})
},
})
如有問題,歡迎留言,與大家共同探討與學習!!!
原文鏈接:https://blog.csdn.net/lff1123/article/details/80254282
相關(guān)推薦
- 2022-08-15 使用mybatis-plus 的baseMapper的selectById 出現(xiàn)查詢不到值
- 2023-10-17 nginx重定向url、nginx域名轉(zhuǎn)發(fā)
- 2022-05-12 Android 記錄build打包的時間并顯示到手機上面
- 2022-02-18 Zabbix Database error
- 2022-06-11 golang實現(xiàn)簡單工廠、方法工廠、抽象工廠三種設(shè)計模式_Golang
- 2022-07-03 對比分析BN和dropout在預測和訓練時區(qū)別_python
- 2022-04-06 Go?字符串比較的實現(xiàn)示例_Golang
- 2022-07-01 docker搭建mongodb單節(jié)點副本集的實現(xiàn)_docker
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支