【发布时间】:2017-11-06 15:49:57
【问题描述】:
我正在开发一个将数据发布到服务器的 Django 应用程序。
这里是 Python sn-p:
def save(request):
if request.method != 'POST':
return HttpResponse('Illegal Request')
print(request.POST.get('dataObj'))
return JsonResponse({'status':200})
客户端的JQuery:
function submitQuoationClicked()
{
dataObj = getQuotationInJSON()
console.log(dataObj)
token = $("input[name=csrfmiddlewaretoken]").val()
$.ajax({
type:"POST",
url:"/quotations/save",
data: {
'csrfmiddlewaretoken': token,
'dataObj' : dataObj,
},
success: function(data){
if(data.status === 200)
console.log("Good Work Falak!") //TODO Remove this line, FOR DEBUGGING ONLY
}
});
function getQuotationInJSON()
{
var data = {};
data['title'] = $("#title").val()
data['companyName'] = $("#company").val()
data['addressLine1'] = $("#addressLine1").val()
data['addressLine2'] = $("#addressLine2").val()
data['city'] = $("#city").val()
data['country'] = $("#country").val()
data['date'] = $("#date").val()
data['number'] = $("#number").val()
data['validTill'] = $("#validTill").val()
sections = getItemsData();
data['sections'] = sections
return data
}
function getItemsData()
{
sections = []
$("tr[id^='section']").each(function(index, element)
{
sectionId = $(element).attr('id').split('section')[1]
name = $(element).find("#sectionName" + sectionId).val()
section = {}
section['name'] = name
//Now get section items
rowElems = $(".section" + sectionId)
rows = []
$(rowElems).each(function(index, row){
if($(row).attr('id').indexOf('itemRow')>=0)
{
description = $(row).find("#description").val()
quantity = $(row).find("#quantity").val()
unitPrice = $(row).find("#unitPrice").val()
margin = $(row).find("#margin").val()
if(quantity!="" && unitPrice!="" && description!="" && margin!="")
{
row = {}
row['description'] = description
row['quantity'] = quantity
row['unitPrice'] = unitPrice
row['margin'] = margin
rows.push(row)
}
}
});
section['items'] = rows;
sections.push(section)
});
return sections
}
JSON 对象是一个包含一些基本细节的引用。它有一些部分,每个部分都有一个名称和该部分中的项目列表。每个项目都有一些属性。但是 print(request.body) 返回“无”。如果我打印(request.body),它会打印以下文本:
b'csrfmiddlewaretoken = ph8NQTz5uDtYaTSI5bDVAcdQ0MQtPrYWMMpIFOYP5eqD8urd2wtPRtxypBSnNg1J&dataObj%5Btitle%5D =标题&dataObj%5BaddressLine1%5D =&dataObj%5BaddressLine2%5D =&dataObj%5Bcity%5D =&dataObj%5Bcountry%5D =&dataObj%5Bdate%5D =&dataObj%5Bnumber%5D =&dataObj %5BvalidTill%5D=&dataObj%5Bsections%5D%5B0%5D%5Bname%5D=S1&dataObj%5Bsections%5D%5B0%5D%5Bitems%5D%5B0%5D%5Bdescription%5D=嘿&dataObj%5Bsections%5D%5B0%5D %5Bitems%5D%5B0%5D%5Bquantity%5D=2&dataObj%5Bsections%5D%5B0%5D%5Bitems%5D%5B0%5D%5BunitPrice%5D=9&dataObj%5Bsections%5D%5B0%5D%5Bitems%5D%5B0 %5D%5Bmargin%5D=0'
我试图从 request.POST.get('dataObj) 中获取 'dataObj' 但它返回 None。
导致问题的原因是什么?
【问题讨论】:
-
为了排除故障,这里是解码后的正文:
b'csrfmiddlewaretoken=ph8NQTz5uDtYaTSI5bDVAcdQ0MQtPrYWMMpIFOYP5eqD8urd2wtPRtxypBSnNg1J&dataObj[title]=Title&dataObj[addressLine1]=&dataObj[addressLine2]=&dataObj[city]=&dataObj[country]=&dataObj[date]=&dataObj[number]=&dataObj[validTill]=&dataObj[sections][0][name]=S1&dataObj[sections][0][items][0][description]=Hey&dataObj[sections][0][items][0][quantity]=2&dataObj[sections][0][items][0][unitPrice]=9&dataObj[sections][0][items][0][margin]=0' -
是的,我也得到了这种格式,但我认为我的 JSON 对象或发送到服务器的格式存在严重问题。我想不通。
标签: jquery python json ajax django