當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
django 使用Ajax方式POST JSON数据包
生活随笔
收集整理的這篇文章主要介紹了
django 使用Ajax方式POST JSON数据包
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
?示例1:
js:
function SaveAction(){//點(diǎn)擊 保存按鍵var senddata = {"type":"A", "host":"www", "resolution_line":"0", "data":"172.16.2.3", "mx":"5", "ttl":"600"}$.ajax({url: "/dns/add.html",type: "POST", //請(qǐng)求類型data: {"data":senddata},dataType: "json", // 這里指定了 dateType 為json后,服務(wù)端響應(yīng)的內(nèi)容為json.dumps(date),下面 success 的callback 數(shù)據(jù)無需進(jìn)行JSON.parse(callback),已經(jīng)是一個(gè)對(duì)象了,如果沒有指定dateType則需要執(zhí)行 JSON.parse(callback)success: function (callback) {//當(dāng)請(qǐng)求執(zhí)行完成后,自動(dòng)調(diào)用//callback, 服務(wù)器返回的數(shù)據(jù) console.log(callback);},error: function () {//當(dāng)請(qǐng)求錯(cuò)誤之后,自動(dòng)調(diào)用 }}); }?
?
?
django 后臺(tái) view:
?
def record_add(req):"""添加解析記錄:param req::return:"""if req.method == 'POST':type = req.POST.get('data[type]')host = req.POST.get('data[host]')resolution_line = req.POST.get('data[resolution_line]')data = req.POST.get('data[data]')mx = req.POST.get('data[mx]')ttl = req.POST.get('data[ttl]')print(type, host, resolution_line, data, mx, ttl)return HttpResponse('hehe')else:return HttpResponse('use POST request method please.')?
?
示例2:
JS
?
function SaveAction(){//點(diǎn)擊 保存按鍵var _type = $(".modal-body select[name=type]").val();var _host = $(".modal-body input[name=host]").val();var _resolution_line = $(".modal-body select[name=resolution_line]").val();var _data = $(".modal-body input[name=data]").val();var _mx = $(".modal-body input[name=mx]").val();var _ttl = $(".modal-body select[name=ttl]").val();var senddata = {"type":_type, "host":_host, "resolution_line":_resolution_line, "data":_data, "mx":_mx, "ttl":_ttl }$.ajax({url: "/dns/add.html",type: "POST", //請(qǐng)求類型//contentType: "application/json; charset=utf-8", data: senddata,dataType: "json",success: function (callback) {//當(dāng)請(qǐng)求執(zhí)行完成后,自動(dòng)調(diào)用//arg, 服務(wù)器返回的數(shù)據(jù) console.log(callback);},error: function () {//當(dāng)請(qǐng)求錯(cuò)誤之后,自動(dòng)調(diào)用 }}); }?
?
?
?
?
django view
def record_add(req):"""添加解析記錄:param req::return:"""if req.method == 'POST':type = req.POST.get('type')host = req.POST.get('host')resolution_line = req.POST.get('resolution_line')data = req.POST.get('data')mx = req.POST.get('mx')ttl = req.POST.get('ttl')print(type, host, resolution_line, data, mx, ttl)return HttpResponse('hehe')else:return HttpResponse('use POST request method please.')?
?
?
示例3:?
js
function SaveAction(){//點(diǎn)擊 保存按鍵var _type = $(".modal-body select[name=type]").val();var _host = $(".modal-body input[name=host]").val();var _resolution_line = $(".modal-body select[name=resolution_line]").val();var _data = $(".modal-body input[name=data]").val();var _mx = $(".modal-body input[name=mx]").val();var _ttl = $(".modal-body select[name=ttl]").val();var senddata = {"type":_type, "host":_host, "resolution_line":_resolution_line, "data":_data, "mx":_mx, "ttl":_ttl }$.post("/dns/add.html", {'data':JSON.stringify(senddata)},function(callback){JSON.parse(callback);}); }?
$.post 源碼:
jQuery.each( [ "get", "post" ], function( i, method ) {jQuery[ method ] = function( url, data, callback, type ) {// shift arguments if data argument was omittedif ( jQuery.isFunction( data ) ) {type = type || callback;callback = data;data = undefined;}return jQuery.ajax({url: url,type: method,dataType: type,data: data,success: callback});}; });?
?
?
?
?
?
django view
def record_add(req):"""添加解析記錄:param req::return:"""if req.method == 'POST':data = req.POST.get('data') # str: {"type":"A","host":"www","resolution_line":"0","data":"172.16.2.3","mx":"5","ttl":"600"}data = json.loads(data) # 字典:{'resolution_line': '0', 'data': '172.16.2.3', 'host': 'www', 'mx': '5', 'ttl': '600', 'type': 'A'}return HttpResponse('hehe')else:return HttpResponse('use POST request method please.')?
?
示例4
js $.ajax post提交 json數(shù)據(jù)
function SaveAction(){//點(diǎn)擊 保存按鍵var _type = $(".modal-body select[name=type]").val();var _host = $(".modal-body input[name=host]").val();var _resolution_line = $(".modal-body select[name=resolution_line]").val();var _data = $(".modal-body input[name=data]").val();var _mx = $(".modal-body input[name=mx]").val();var _ttl = $(".modal-body select[name=ttl]").val();var senddata = {"type":_type, "host":_host, "resolution_line":_resolution_line, "data":_data, "mx":_mx, "ttl":_ttl }$.ajax({url: "/dns/add.html",type: "POST", //請(qǐng)求類型contentType: "application/json; charset=utf-8",data: JSON.stringify(senddata),dataType: "json",success: function (callback) {//當(dāng)請(qǐng)求執(zhí)行完成后,自動(dòng)調(diào)用//arg, 服務(wù)器返回的數(shù)據(jù) console.log(callback);},error: function () {//當(dāng)請(qǐng)求錯(cuò)誤之后,自動(dòng)調(diào)用 }}); }?
?
?
?
?
django view
import jsondef record_add(req):"""添加解析記錄:param req::return:"""if req.method == 'POST':body_data = req.body # 獲取http請(qǐng)求體數(shù)據(jù)print(body_data) # b'{"type":"A","host":"www","resolution_line":"0","data":"172.16.2.3","mx":"5","ttl":"600"}'print(type(body_data)) # <class 'bytes'>body_data_str = str(body_data, encoding="utf8") # bytes 轉(zhuǎn) strdata = json.loads(body_data_str)print(data['type'])return HttpResponse('hehe')else:return HttpResponse('use POST request method please.')?
示例5:
建議使用這種方式
js
function SaveAction(){//點(diǎn)擊 保存按鍵var _type = $(".modal-body select[name=type]").val();var _host = $(".modal-body input[name=host]").val();var _resolution_line = $(".modal-body select[name=resolution_line]").val();var _data = $(".modal-body input[name=data]").val();var _mx = $(".modal-body input[name=mx]").val();var _ttl = $(".modal-body select[name=ttl]").val();var senddata = {"type":_type, "host":_host, "resolution_line":_resolution_line, "data":_data, "mx":_mx, "ttl":_ttl }$.ajax({url: "/dns/add.html",type: "POST", //請(qǐng)求類型data: {'data': JSON.stringify(senddata)},dataType: "json",success: function (callback) {//當(dāng)請(qǐng)求執(zhí)行完成后,自動(dòng)調(diào)用//arg, 服務(wù)器返回的數(shù)據(jù) console.log(callback);},error: function () {//當(dāng)請(qǐng)求錯(cuò)誤之后,自動(dòng)調(diào)用 }});}?
?
?js post提交源數(shù)據(jù):
?
?
?django view
def record_add(req):"""添加解析記錄:param req::return:"""if req.method == 'POST':data = req.POST.get('data')print(type(data)) # <class 'str'>data = json.loads(data) # str json序列化return HttpResponse('hehe')else:return HttpResponse('use POST request method please.')?
轉(zhuǎn)載于:https://www.cnblogs.com/linkenpark/p/7461709.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的django 使用Ajax方式POST JSON数据包的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (转)spring aop(下)
- 下一篇: Spring boot centos部署