jQuery的ajax的文章列表附加“[]”在Django到的QueryDict文章列表、ajax、jQuery、Django

由网友(孤傲的白狼)分享简介:我工作的一个Django项目,我送通过jQuery的Ajax方法的post请求。该csrftoken已被检索从浏览器的JavaScript的Cookie。 I am working on a Django project and I am sending a post request via Jquery's aja...

我工作的一个Django项目,我送通过jQuery的Ajax方法的post请求。该csrftoken已被检索从浏览器的JavaScript的Cookie。

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);
});

然后我检查通过Ajax调用哪些后端接收;

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']}>

这似乎很奇怪。哪儿来的[]多余的键名从何而来?这是处理在Django这种方式还是这东西发送清单时AJAX做了约定?

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?

推荐答案

这是jQuery的认为世界上每个人使用PHP。

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

添加传统:真正的 AJAX 对象

阅读全文

相关推荐

最新文章