0

I am trying to send Javascript Array via AJAX POST to django view.

I store this array in hidden input using JSON.stringify:

$('#id_uuids').val(JSON.stringify(arr));

this is how I try to send it:

$.post("/ajax/generateGallery",{uuids: $('#id_uuids').val()},function(response){
    resp = JSON.parse(response);
    alert(resp.html);
},"json");

Browser console shows that data which is being send looks like:

uuids:["6ecbe35b-0b77-4810-aa9a-918fecaeef13","e41f52f7-721b-4d44-b7d6-bbb275182d66"]

However, I am not able to use this in my django view. I've tried:

uuids = request.POST.getlist('uuids')
logger.info(uuids)
logger.info(type(uuids))

which returns:

[08/Aug/2014 15:20:00] INFO [app.rest_client:307] [u'["89a26646-6000-4c48-804a-69abcc496fd8"]']
[08/Aug/2014 15:20:00] INFO [app.rest_client:308] <type 'list'>
[08/Aug/2014 15:20:00] INFO [app.rest_client:312] Generate HTML gallery for photo ["89a26646-6000-4c48-804a-69abcc496fd8"]

So, Django treats very list sent as single element. How can I force python code to treat this data as list and be able to iterate on?

1
  • The problem is that you stringify the array, and it's submitted as string, this is not a Django issue but rather how you handle values in JS. Commented Aug 8, 2014 at 13:37

1 Answer 1

1

try to JSON-decode the posted data

uuids = json.loads(request.POST.get('uuids'))

that is if you loaded some json module before, e.g.

import simplejson as json
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.