【问题标题】:How do I send/get a JSON Object with Django?如何使用 Django 发送/获取 JSON 对象?
【发布时间】:2010-01-07 20:52:46
【问题描述】:

我知道如何使用 JQuery ajax 功能来调用 Django 的“url 视图”。

import simplejson as json
def the_view(request):
    fruits = {'color':5, 'type': 22}
    jfruit = json.dump(fruits)

return render_to_response( THE JSON OBJECT!!! ...how? )

【问题讨论】:

    标签: python django json


    【解决方案1】:

    返回 HttpResponse(simplejson.dumps(mydictionary), mimetype="application/json")

    b-list

    【讨论】:

    • 虽然 JSON mimetype 确实是 application/json
    • 好点,我改了。实际上,这只是为了消除返回默认“text/html”mimetype 的 XSS 安全问题,所以两者都可以。谢谢:)
    【解决方案2】:

    或更短: 下载http://bitbucket.org/offline/django-annoying/并写:

    @ajax_request 
    def the_view(request):
        return {'color':5, 'type': 22}
    

    在 django-annoying 中有一些非常棒的小东西。

    【讨论】:

      猜你喜欢
      • 2017-10-28
      • 2020-12-16
      • 2019-10-13
      • 2020-09-26
      • 1970-01-01
      • 2016-06-07
      • 2017-08-16
      • 2014-02-07
      • 1970-01-01
      相关资源
      最近更新 更多