I am working on a Django project and I am sending a post request via Jquery's ajax method. The csrftoken has been retrieved from the browsers cookie with javascript.
$.ajax({
type : 'POST',
beforeSend: function( xhr, settings){
xhr.setRequestHeader("X-CSRFToken", csrftoken );
},
url : '/endpoint/',
data : { 'requestParam': [1,2,3,4] }
}).done(function(d) {
callback(d);
});
Then I check what the backend receives via the ajax call;
print( request.POST )
I was expecting this;
<QueryDict: {u'requestParam': [u'1', u'2', u'3', u'4']}>
Instead I get this;
<QueryDict: {u'requestParam[]': [u'1', u'2', u'3', u'4']}>
Which seems odd. Where did the '[]' extra in the key name e from? Is this a convention that is handled this way in Django or is this something that AJAX does when sending lists?
I am working on a Django project and I am sending a post request via Jquery's ajax method. The csrftoken has been retrieved from the browsers cookie with javascript.
$.ajax({
type : 'POST',
beforeSend: function( xhr, settings){
xhr.setRequestHeader("X-CSRFToken", csrftoken );
},
url : '/endpoint/',
data : { 'requestParam': [1,2,3,4] }
}).done(function(d) {
callback(d);
});
Then I check what the backend receives via the ajax call;
print( request.POST )
I was expecting this;
<QueryDict: {u'requestParam': [u'1', u'2', u'3', u'4']}>
Instead I get this;
<QueryDict: {u'requestParam[]': [u'1', u'2', u'3', u'4']}>
Which seems odd. Where did the '[]' extra in the key name e from? Is this a convention that is handled this way in Django or is this something that AJAX does when sending lists?
Share Improve this question asked Jan 9, 2014 at 9:46 cantdutchthiscantdutchthis 34.7k17 gold badges77 silver badges116 bronze badges1 Answer
Reset to default 13This is jQuery believing that everyone in the world uses PHP.
Add traditional: true
to your ajax
object.