在django1.11的官網(wǎng)中是這么解釋的:
HttpRequest.body:
The raw HTTP request body as a byte string. This is useful for processing data in different ways than conventional HTML forms: binary images, XML payload etc. For processing conventional form data, use HttpRequest.POST.
原始的http字節(jié)請求。在處理二進制圖片,XML負載時诀拭。當處理的常規(guī)的表單數(shù)據(jù)時,使用HttpRequest.POST
HttpRequest.POST:
A dictionary-like object containing all given HTTP POST parameters, providing that the request contains form data. See the QueryDict documentation below. If you need to access raw or non-form data posted in the request, access this through the HttpRequest.body attribute instead.
It’s possible that a request can come in via POST with an empty POST dictionary – if, say, a form is requested via the POST HTTP method but does not include form data. Therefore, you shouldn’t use if request.POST to check for use of the POST method; instead, use if request.method == "POST" (see HttpRequest.method).
POST does not include file-upload information. See FILES.
request.POST是一個字典一樣的數(shù)據(jù)挤安,見QueryDict。適用于表單,當你需要獲取原始數(shù)據(jù)或者非表單數(shù)據(jù)時,請用request.body夺鲜。該方法文件上傳不包括文件上傳,見HttpRequest.FILES
例子1
ajax中:
$.ajax({
type: "post",
url: 'http://192.168.124.149:8000/tempaction/',
traditional: true , // 1
//contentType: "application/json" , 注意這一行注釋了
dataType: "json",
data: {
type: [1,2,3],
},
success: function (data) {
console.log(data)
}
});
django中print以下reqeust的屬性:
request.content_type -----> application/x-www-form-urlencoded
request.body -----> b'type=1&type=2&type=3'
request.POST -----> <QueryDict: {'type': ['1', '2', '3']}>
request.POST.get['type'] -----> 3
request.POST.getlist----->['1', '2', '3']
- 很多時候呐舔,我們用 Ajax 提交數(shù)據(jù)時币励,也是使用這種方式。例如 JQuery 和 QWrap 的 Ajax珊拼,Content-Type 默認值都是「application/x-www-form-urlencoded;charset=utf-8」.其次食呻,提交的數(shù)據(jù)按照 key1=val1&key2=val2 的方式進行編碼,key 和 val 都進行了 URL 轉(zhuǎn)碼澎现。大部分服務(wù)端語言都對這種方式有很好的支持
- 但是這里標記為1的那一行,如果沒有
traditional: true
這個參數(shù)(很亂,有興趣的可以自己試一下),在java,python后臺是無法取到參數(shù)的仅胞,因為jQuery需要調(diào)用jQuery.param序列化參數(shù)jQuery.param( obj, traditional )
默認的話,traditional為false剑辫,即jquery會深度序列化參數(shù)對象干旧,以適應(yīng)如PHP和Ruby on Rails框架, 但servelt api,python之類的無法處理妹蔽,我們可以通過設(shè)置traditional 為true阻止深度序列化椎眯,然后序列化結(jié)果就如上面的一樣了- 關(guān)于request.POST.get['type']值為3,為什么只取最后一個值,可取django官網(wǎng)查詢,這也就有了下面那個
getlist()
方法.但是有瑕疵,因為得到的list中的元素都是字符串,要自行轉(zhuǎn)換,稍顯麻煩
例子2
ajax中:
$.ajax({
type: "post",
url: 'http://192.168.124.149:8000/tempaction/',
contentType: "application/json" ,
dataType: "json",
data: JSON.stringify(({ //2
type: 'add',
person: [
{
id: '1',
info: ['andy', '22']
}, {
id: '2',
info: ['lily', '21']
}
]
})),
django中print以下reqeust的屬性:
request.content_type -----> application/json
request.body -----> b'{"type":"add","person":[{"id":"1","info":["andy","22"]},{"id":"2","info":["lily","21"]}]}'
json.loads(request.body.decode('utf-8')) ------> 一個完整數(shù)據(jù),即python字典
request.POST -----> <QueryDict: {}>
- 說一下標記的2,
JSON.stringify:The JSON.stringify() method converts a JavaScript value to a JSON string,
.http傳輸中用的是字符串,準確的來說是json字符串,這個方法將一個js對象裝換為json字符串,傳輸?shù)胶蠖?后端接收,然后解碼后再反序列化一下,就獲得數(shù)據(jù)了.這樣的話后端傳到前端應(yīng)該也是這么個道理 =.=,不知道這種說法有沒有問題- 此時request.POST中為空
總結(jié)
- request.POST適用于接收表單數(shù)據(jù),表單數(shù)據(jù)一般是比較簡單,沒有嵌套.面對復(fù)雜的數(shù)據(jù)時,即使像例子1中一個簡單的list接受后都會變形(元素變?yōu)樽址?,處理起來沒那么順手.
- request.body可以方便的提交復(fù)雜的結(jié)構(gòu)化數(shù)據(jù),特別適合 RESTful 的接口
- 因為本人前端知識比較爛,講的都是一些非扯锟基礎(chǔ)的東西,但對于我來說,查閱了一些資料后,受益頗多.有說錯的地方歡迎指點,相互交流.
相關(guān)鏈接
https://ask.helplib.com/436351
http://www.cnblogs.com/aaronjs/p/4165049.html
https://my.oschina.net/i33/blog/119506
http://blog.csdn.net/panyu_smd/article/details/70174126