所以我有这个代码:
def success_comment_post(request):
if "c" in request.GET:
c_id = request.GET["c"]
comment = Comment.objects.get(pk=c_id)
model = serializers.serialize("json", [comment])
data = {'message': "Success message",
'message_type': 'success',
'comment': model }
response = JSONResponse(data, {}, 'application/json')
return response
else:
data = {'message': "An error occured while adding the comment.",
'message_type': 'alert-danger'}
response = JSONResponse(data, {}, 'application/json')
回到 jQuery 中,我执行以下操作:
$.post($(this).attr('action'), $(this).serialize(), function(data) {
var comment = jQuery.parseJSON(data.comment)[0];
addComment($("#comments"), comment);
})
现在......在Django函数中,为什么我必须把注释放在[] --> model = serializers.serialize("json", [comment])
回到 jQuery,为什么我必须做 jQuery.parseJSON(data.comment) [0]?
无论如何我不必这样做?我觉得很奇怪我必须硬编码[0]
非常感谢!