最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery ajax post list appends '[]' to querydict in django - Stack Overflow

programmeradmin2浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 13

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

Add traditional: true to your ajax object.

发布评论

评论列表(0)

  1. 暂无评论