1
我想通过更新ajax请求中的内容来做网站。我收到JSON数据,我必须把这个JSON数据放到MDL CARD中。 http://www.getmdl.io/components/index.html#cards-section向mdl卡添加ajax响应
我的Ajax代码:
$(document).ready(function(){
displayRecords(limit, offset);
$('#button').click(function(){
limit = limit + 2;
offset = offset + 2;
displayRecords(limit, offset);
});
});
function displayRecords(lim, off){
$.ajax({
type: "GET",
url: "/main/",
data: {'limit': lim, 'offset': off},
dataType: 'json',
success: function(data) {
for (var post in data){
$('.mdl-card__supporting-text').append(data[post].fields.title);
}
}
});
}
和我的Django代码:
def posts(request):
if request.is_ajax():
offset = request.GET.get('offset')
limit = request.GET.get('limit')
all_posts = Post.objects.all()[offset:limit]
data = serializers.serialize('json', all_posts, ensure_ascii=False)
print data
return HttpResponse(data, content_type='application/json; charset=utf8')
else:
return render(request, 'main.html')
您有任何疑问/问题? –
是的,我该怎么做?我如何将json数据放入mdl卡? –