简体   繁体   中英

django flatten dictionaries in ajax get / post

I'm trying to send simple ajax request to django via get/post (doesn't matter) and it seems like django flattens the dictionary when parsing the request.

With example:

What I send to django in ajax (via jquery):

{'payload':{'website':'localhost.com'}}

What I get in request.post on django side:

{u'payload[website]': [u'localhost.com']}

Why?

thanks,

EDIT: added jquery call

$.ajax({
    type: "GET",
    url: full_url,
    data: {'payload': {'website': 'localhost.com'}},
    dataType: "json",
    contentType: "application/json",
    async: true,
    timeout: 5000,
});

What content-type are you sending to the django server? It should be set to : 'application/json' so the server knows it is receiving json. Also, try to retrieve the data with request.data instead of data.post. You should then be able to do the following:

if (request.data):
   payload = request.data['payload']

Quick note, if thats all you are passing to the server i would remove one level and only send the inner dictionnary like so :

{'website':'localhost.com'}

and then retrieve it with

request.data['website']

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