我想渲染兩個不同的HTML樣本,並將其作爲對ajax請求的響應發回。如何在html中將兩個變量作爲JSON發送給Django?
我有我的看法是這樣的:
def getClasses(request):
User = request.user
aircomcode = request.POST.get('aircompany_choice', False)
working_row = Pr_Aircompany.objects.get(user=User, aircomcode=aircomcode)
economy_classes = working_row.economy_class
business_classes = working_row.business_class
economy = render_to_response('dbmanager/classes.html', {"classes": economy_classes}, content_type="text/html")
business = render_to_response('dbmanager/classes.html', {"classes": business_classes}, content_type="text/html")
return JsonResponse({"economy": economy,
"business": business})
有了這個,我得到的錯誤:
在0x7f501dc56588django.http.response.HttpResponse對象不是JSON序列化」
我怎樣才能完成我的任務?
在js當我得到響應我想插入接收的HTML到corespoding塊。像這樣:
$.ajax({ # ajax-sending user's data to get user's classes
url: url,
type: 'post',
data: {"aircompany_choice": aircompany_choice}, # send selected aircompanies for which to retrieving classes required
headers: {"X-CSRFToken":csrftoken}, # prevent CSRF attack
}).done (result) ->
add_booking_classes.find(".economy-classes").children(":nth-child(2)").html(result["economy"])
add_booking_classes.find(".business-classes").children(":nth-child(2)").html(result["business"])
在JSON你不應該發送HTML,它是一個不錯的辦法...... – madzohan