简体   繁体   中英

How to send the javascript dictionary object to django view?

I am trying to send the ajax calls to server using some data. Here is my javascript code,

for (var i = 0; i < resend_data['tags'].length; i++) {                          
   dic_tags[resend_data['tags'][i]] = $("#_id_"+resend_data['tags'][i]).val()

};

data= {'creation_tags': dic_tags, 'description': '', 'title': '', 'pop_flag': true}

But in the server side I am getting like this,

<QueryDict: {u'description[]': [u''], u'title[]': [u''], u'pop_flag': [u'true'], u'creation_tags[0][e]': [u'1'], u'creation_tags[0][d]': [u'1']}>

But I want the dictionary like this,

u'creation_tags[]':[{'e':'1','d':'1'}]

You can also send the tag list as shown below,

for (var i = 0; i < resend_data['tags'].length; i++) {                          
   dic_tags[resend_data['tags'][i]] = $("#_id_"+resend_data['tags'][i]).val()

};

data= {'creation_tags': dic_tags, 'description': '', 'title': '', 'pop_flag': true, 'tags': resend_data['tags']}

Server side,

tag_dict = {}

for tag in request.POST.getlist('tags[]'):
    tag_dict[tag] = request.POST.getlist('creation_tags['+ tag +']')

print tag_dict

Output:

{'e':'1','d':'1'}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM