小程序怎样和服务器交互,微信小程序与 Django服务器 数据流 交互通信
Django服務(wù)器 解析小程序發(fā)送的json二進(jìn)制字符串
https://blog.csdn.net/qq_25068917/article/details/81041048
http://www.cnblogs.com/Liang-jc/p/9326680.html
小程序 解析Django服務(wù)器返回的json二進(jìn)制字符串(字典)
https://blog.csdn.net/qiushi_1990/article/details/75912945
總結(jié):
django服務(wù)端:(獲取和返回)
獲取post提交的數(shù)據(jù)2種情況
1?獲取post的鍵值數(shù)據(jù):
小程序header?設(shè)置為??"Content-Type": "application/x-www-form-urlencoded"
2?獲取post的json格式的數(shù)據(jù):
小程序header?設(shè)置為??"Content-Type": "application/json
詳細(xì)介紹:http://www.cnblogs.com/zhangdewang/p/9222952.html
1獲取客戶端數(shù)據(jù),是第二種情況:
接受到post的json格式的數(shù)據(jù),用json.loads解析json二進(jìn)制字符串
postBody = request.body
json_result = json.loads(postBody)
name =json_result['name']
2?向客戶端返回?cái)?shù)據(jù),返回字典格式的數(shù)據(jù)
dic = {
'name': user.name,
'email': user.email,
'num': user.number
}
return JsonResponse(dic)
注:關(guān)于JsonResponse(dic)的詳細(xì)介紹:
JsonResponse?對(duì)象:
class JsonResponse(data, encoder=DjangoJSONEncoder,?safe=True, json_dumps_params=None,**kwargs)
這個(gè)類是HttpResponse的子類,它主要和父類的區(qū)別在于:
1?默認(rèn)Content-Type?被設(shè)置為:application/json
2 data參數(shù):data應(yīng)該是一個(gè)字典類型,
3 safe?參數(shù):true,false
默認(rèn)參數(shù)是True:?傳入的data數(shù)據(jù)類型不是字典類型,就會(huì)拋出TypeError異常。
設(shè)置為False?:data可以填入任何 能被轉(zhuǎn)換為JSON格式的對(duì)象,如list, tuple, set
https://www.cnblogs.com/guoyunlong666/p/9099397.html
微信小程序客戶端:
1?發(fā)送數(shù)據(jù)時(shí)
data{}??header?設(shè)置為?"Content-Type": "application/json"
2?接收服務(wù)器的返回?cái)?shù)據(jù)時(shí),
1?js:獲取字典res.data.name?,通過dic.name?獲取字典的value,
setdata傳遞給 綁定變量, 傳遞給wxml,顯示
2??js:獲取字典res.data,setdata傳遞給 綁定變量, 再傳遞給wxml
wxml:通過綁定變量.name?獲取字典的value,顯示
Django獲取數(shù)據(jù)
request.body:類型和內(nèi)容
(Json二進(jìn)制串)
b'{"name":"\xe7\x94\xa8\xe6\x88\xb73","email":"qwew222","num":"321222"}'
json.loads(postBody):
{'name': '用戶3', 'email': 'qwew222', 'num': '321222'}
request.body返回bytes類型。
如何獲取 請(qǐng)求體中的如下JSON數(shù)據(jù)
{"name": "fenghua", "age": 24}
方法:獲取到的是原始二進(jìn)制數(shù)據(jù),要進(jìn)行解碼
(字符串化,得到j(luò)son數(shù)據(jù))json_dict化(將json數(shù)據(jù)轉(zhuǎn)換成字典)
本文來自qq_25068917?的CSDN?博客 ,全文地址請(qǐng)點(diǎn)擊:https://blog.csdn.net/qq_25068917/article/details/81041048?utm_source=copy
總結(jié)
以上是生活随笔為你收集整理的小程序怎样和服务器交互,微信小程序与 Django服务器 数据流 交互通信的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql shell 所有表_删除my
- 下一篇: chown -r oracle:oins