字典对象从 jQuery 到 Django 的混淆!
我正在尝试使用 getJSON 调用将字典从 jQuery 发送到 Django:
jQuery.getJSON(URL,JSONData,function(returnData){});
JSONData 对象的格式如下:
JSONData = {
year:2010101,
name:"bob",
data:{
search:[jim,gordon],
register:[jim],
research:[dave],
}
}
这是以编程方式组合在一起的,但看起来不错。
一旦传递到 Django,“年份”和“名称”对象就如预期的那样。然而,数据对象包含以下键/值 - "search[0]":"jim", "search[1]":"gordon","register[0]":"jim","research[0]" :“dave”,而不是预期的“search”:(数据数组)、“register”:(数据数组)、“research”:(数据数组)。
如果我使用对象代替数组,也会发生类似的情况。
这是 Django 对对象的解释的问题吗?
知道我该如何纠正这个问题......干净吗?
编辑:
我现在简化了数据以使测试更容易:
JSONData = {
year:2010101,
name:"bob",
search:[jim,gordon],
register:[jim],
research:[dave],
}
I'm attempting to send a dictionary from jQuery to Django using a getJSON call:
jQuery.getJSON(URL,JSONData,function(returnData){});
The JSONData object is formatted as follows:
JSONData = {
year:2010101,
name:"bob",
data:{
search:[jim,gordon],
register:[jim],
research:[dave],
}
}
This is put together programmatically but looks fine.
Once passed to Django the "year" and "name" objects are as expected. The data object however contains the following keys/values - "search[0]":"jim", "search[1]":"gordon","register[0]":"jim","research[0]":"dave", rather than the expected "search":(array of data), "register":(array of data), "research":(array of data).
Similar things happen if I use objects in place of the arrays.
Is this an issue with Django's interpretation of the object?
Any idea how I might correct this...cleanly?
EDIT:
I have now simplified the data to make testing easier:
JSONData = {
year:2010101,
name:"bob",
search:[jim,gordon],
register:[jim],
research:[dave],
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
request.GET
不是普通 pythondict
的实例,而是 django 类QueryDict
,可以处理一个键的多个值。如果您需要作为列表返回的键的多个值,则必须使用getList
!编辑:另请查看jQuery 参数设置!
request.GET
is not an instance of a normal pythondict
, but of the django classQueryDict
, that can deal with multiple values for one key. If you need multiple values for a key returned as a list you have to usegetList
!EDIT: Also have a look at this jQuery parameter settings!