【问题标题】:How to print POSTed json on html in Django如何在 Django 中的 html 上打印 POSTed json
【发布时间】:2014-07-22 19:03:05
【问题描述】:

这是我的index.htmljavascript:

$('#clickme').bind('click',function(){

 var url = "/lms/quizes/show/";
        var data = {"size": "width=8, height=9", "weight": "8 kg", "desc": "'&='"};
        var string_data = JSON.stringify(data);

        $.ajax({
        type: "POST",
        url: url,
        async: true,
        data: JSON.stringify(data),
        success: function(response) {
            alert(JSON.stringify(data));
        }
        });
})

这是我的views.py:

@csrf_exempt
def show(request):
    if request.is_ajax():
        if request.method == 'POST':
            print 'Raw Data: "%s"' % request.body   
    return HttpResponse(request, content_type="application/json")

我想将 json 发布到 url show.html,但现在我只能在控制台日志中看到 json。

【问题讨论】:

  • 对于初学者,只需返回 request.body 而不是 OK

标签: javascript python json django


【解决方案1】:

而不是

return HttpResponse("OK")

试试

return HttpResponse(request, content_type="application/json")

【讨论】:

  • @Zach Spencer 它只在 index.html 的控制台中显示数据,而不在 show.html 中打印
  • 我很难理解您的需求,很抱歉。
猜你喜欢
  • 2017-12-29
  • 2012-11-20
  • 1970-01-01
  • 1970-01-01
  • 2013-11-28
相关资源
最近更新 更多