【问题标题】:Add ajax response to mdl card向 mdl 卡添加 ajax 响应
【发布时间】:2016-01-09 05:56:41
【问题描述】:

我想通过 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')

【问题讨论】:

  • 您有任何疑问/问题吗?
  • 是的,我该怎么做?我如何将 json 数据放入 mdl 卡?

标签: jquery json ajax django


【解决方案1】:

我使用了 mustache.js。它对我有帮助。

【讨论】:

  • 你是如何使用它的?能举几个例子吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-04
  • 1970-01-01
  • 2016-06-26
  • 2021-06-23
  • 1970-01-01
  • 2020-12-01
  • 1970-01-01
相关资源
最近更新 更多