0
所以我有這樣的代碼:JSON序列化的Django和JSON解析jQuery的
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的功能,爲什麼我把評論中[] - > 模型= serializers.serialize( 「JSON」,[評論])
回到jQuery,爲什麼我必須要做jQuery.parseJSON(data.comment)[0]?
無論如何,我不必這樣做?我覺得很奇怪,我必須硬編碼[0]
非常感謝!
數據以數組形式出現,使用[0]確保您正在讀取數組的第一個元素 – 2012-11-02 12:26:30
反正它不會出現數組?我真的只是傳遞一個對象。 – abisson