On 23/08/2013 13:30, Sarfraz Ahmad wrote:
Hello guys, i am working on a view where i m needed to pass an array of
objects into a ajax request.
the array of java script objects is like this
[{'que':1,'ans':3},{'que':1,'ans':2},{'que':4,'ans':5},{'que':2,'ans':4}]
i posted is in ajax request like
$.ajax({
url:'/someurl/',
type:'POST',
data:{'array':[{'que':1,'ans':3},{'que':1,'ans':2},{'que':4,'ans':5},{'que':2,'ans':4}]},
success:function(data){},
error:function(data){}
})
but when i recieve it in view using
request.POST.get('array') or
request.POST.getlist('array') or
request.POST.getlist('array[]')
It gives me a blank list not the data i have sent with the request.
tell me how can i get this data in django view.
any help would be appreciable
Hi,
You can serialize your javascript object in JSON and POST the resulting
string to Django. Then in your view, decode the json POSTed string to a
list of dict.
Javascript:
var s =
JSON.stringify([{'que':1,'ans':3},{'que':1,'ans':2},{'que':4,'ans':5},{'que':2,'ans':4}]);
$.ajax({
url:'/someurl/',
type:'POST',
data:{'s': s},
success:function(data){},
});
Django view:
s = request.POST['s']
o = json.loads(s)
--
Laurent Meunier <laur...@deltalima.net>
--
You received this message because you are subscribed to the Google Groups "Django
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.