在Django中处理Ajax json对象-'QueryDict'对象没有属性'read'错误 [英] Handling ajax json object in Django - 'QueryDict' object has no attribute 'read' error

查看:142
本文介绍了在Django中处理Ajax json对象-'QueryDict'对象没有属性'read'错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Django视图中解析json对象,该对象已由ajax通过post方法从客户端传递过来.

I am trying to parse json object in my Django view which has been passed through from client by ajax via post method.

JS:

$.post ('/update_vendor_merchandise_types/', JSON.stringify(json_obj));

查看:

def update_vendor_merchandise_types(request):
    print json_object
    # The output gives me  
    # QueryDict: <QueryDict: {u'[{"merchandise_id":"3"},{"merchandise_id":"4"}]': [u'']}>
    json_object = json.load(request.POST) # Error arises
pass

在注释行上出现'QueryDict' object has no attribute 'read' error. 我在做什么错了?

On the commented line 'QueryDict' object has no attribute 'read' error arises. What am I doing wrong ?

最终,我的目标是访问merchandise_id值.我尝试

Eventually, my goal is to get access to merchandise_id values. I try

d = request.POST.iteritems()
for key, value in d:
    print value

并期望类似

3 
4

推荐答案

request.POST用于表单编码的内容.对于JSON,您应该直接访问普通主体:

request.POST is for form-encoded content. For JSON, you should access the plain body directly:

json_object = json.loads(request.body)

这篇关于在Django中处理Ajax json对象-'QueryDict'对象没有属性'read'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆