cantdutchthis
cantdutchthis

Reputation: 34617

jquery ajax post list appends '[]' to querydict in django

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 come from? Is this a convention that is handled this way in Django or is this something that AJAX does when sending lists?

Upvotes: 1

Views: 1829

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599876

This is jQuery believing that everyone in the world uses PHP.

Add traditional: true to your ajax object.

Upvotes: 13

Related Questions