将json对象从ajax调用返回到django视图

时间:2014-05-29 22:01:10

标签: ajax django

我试图返回一个json对象来渲染到我的模板中的网格。 我就是这样做的。

views.py
def ajax_up(request):
    history_data=Upload_history.objects.all()
    history=serializers.serialize("json",history_data)
    return HttpResponse( history, mimetype='application/json' )
html
 $(".reply").click(function(){

      $.ajax({
    url: "/ajax_up/",
    type: 'GET', //this is the default though, you don't actually need to always mention it
    dataType: "json",
    success: function(data) {
      alert("awasome"+ data)

    },
    failure: function(data) { 
        alert('Got an error');
    }

}); 

所以我宣布一个对象将数据保存为

var data = {{history|safe}};

在上面的视图中从ajax调用返回历史记录 但是当我做警报(数据)时,我得到[对象对象],[对象对象] ..... 请帮忙吗?

1 个答案:

答案 0 :(得分:0)

听起来很有效,但alert只显示一个字符串。由于您的数据不是字符串,因此会显示[object Object]。

使用JSON.stringify序列化数据或使用console.log而不是alert来查看浏览器javascript控制台中的数据。

相关问题