jQuery的ajax的文章列表附加“[]”在Django到的QueryDict [英] jquery ajax post list appends '[]' to querydict in django

查看:140
本文介绍了jQuery的ajax的文章列表附加“[]”在Django到的QueryDict的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个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 对象

这篇关于jQuery的ajax的文章列表附加“[]”在Django到的QueryDict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆