【发布时间】:2017-04-23 22:30:21
【问题描述】:
我有兴趣从views.py 以JSON 格式获取插入操作的结果。我想我得到的结果很好。我的观点是这样的:
added={}
if request.method=='POST':
#Post method initiated.
try:
for id in allergies:
allergy=PatientAllergy(patient=patient,allergy_id=id,addedby=request.user)
allergy.save()
added[allergy.id]=id
except BaseException as e:
pass
return JsonResponse(added,safe=False)
从 JQUERY 传递的记录已成功添加到数据库中。我现在想要得到的是 {12:1, 13:2} 形式的 JSON 结果。
我的 firebag 显示响应为:
12:1
13:2
我不确定这是否是有效的 JSON。如果我列出(添加),它会给出:
0: 12
1: 13
这是我不想要的。我现在遇到的问题是我想通过退回的物品,但我得到的结果不正确。我基本上想得到 12:1, 13:2。
$.ajax({
type: "POST",
url: "/patient/addallergy/",
data: postForm,
dataType : "json",
cache: "false",
success: function (result) {
alert(result.length); //gives undefined, rendering the below line meaningless
if (result.length>0){
$.each(result,function(key,value){
alert(key);
alert(value);
});
}
},
fail: function (result){
}
});
【问题讨论】:
-
"不确定它是否是有效的 json" ...网上有很多 json 验证器
-
感谢 Charlietlf...