添加ajax响应到mdl卡

时间:2015-10-11 20:02:06

标签: jquery json ajax django

我想做一个更新ajax请求内容的网站。我收到json数据,我必须把这个json数据放到MDL CARD。 http://www.getmdl.io/components/index.html#cards-section

我的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')

1 个答案:

答案 0 :(得分:0)

我用过mustache.js。这对我很有帮助。