I currently have the following javascript array:
我目前有以下javascript数组:
var stuffs = ['a', 'b'];
I pass the above to the server code using jQuery's load
:
我使用jQuery的load将上面的内容传递给服务器代码:
var data = {
'stuffs': stuffs
};
$(".output").load("/my-server-code/", data, function() {
});
On the server side, if I print the content of request.POST
(I'm currently using Django), I get:
在服务器端,如果我打印请求的内容。POST(我正在使用Django),我得到:
'stuffs[]': [u'a', u'b']
Notice the []
at the prefix of the variable name stuffs
. Is there a way to remove that []
before it reaches the server code?
注意变量名的前缀[]。是否有办法在[]到达服务器代码之前删除它?
3 个解决方案
#1
32
This is default behavior in jQuery 1.4+...if you want the post to be &stuffs=a&stuffs=b
instead of &stuffs[]=a&stuffs[]=b
you should set the traditional
option to true
, like this:
这是jQuery 1.4+的默认行为……如果你想让邮件为&stuff =a&stuff =b而不是&stuff []=a&stuff []=b,你应该将传统选项设置为true,如下图所示:
$.ajaxSetup({traditional: true});
Note this affects all requests... which is usually what you want in this case. If you want it to be per-request you should use the longer $.ajax()
call and set traditional: true
there. You can find more info about traditional
in the $.param()
documentation.
注意这会影响到所有的请求……这通常是你想要的。如果您希望它是针对每个请求的,您应该使用较长的$.ajax()调用并设置传统的:true。您可以在$.param()文档中找到有关传统的更多信息。
#2
-1
When an array is submitted using a GET request, through a form or AJAX, each element is given the name of the array, followed by a pair of optionally empty square brackets. So the jQuery is generating the url http://example.com/get.php?stuff[]=a&stuff[]=b
. This is the only way of submitting an array, and the javascript is following the standard.
当使用GET请求(通过表单或AJAX)提交数组时,每个元素都被赋予数组的名称,后跟一对可选的空方括号。jQuery生成url http://example.com/get.php?stuff[]=a&stuff[]=b。这是提交数组的唯一方式,javascript遵循标准。
POST
requests work in exactly the same way (unless the json is sent as one long json string).
POST请求的工作方式完全相同(除非json作为一个长json字符串发送)。
In PHP, this is parsed back into the original array, so although the query string can be a little strange, the data is recieved as it was sent. $_GET['stuff'][0]
works correctly in PHP.
在PHP中,这将被解析回原始数组中,因此尽管查询字符串可能有点奇怪,但在发送数据时数据将被接收。$_GET['stuff'][0]在PHP中工作正确。
I'm not sure how Django parses query strings.
我不确定Django如何解析查询字符串。
#3
-3
The []
indicates that the variable is an array. I imagine that the appending of the []
to your variable name is Python/Django's way of telling you it is an array. You could probably implement your own print function which does not show them.
[]表示该变量是一个数组。我认为在变量名后面加上[]是Python/Django告诉您它是一个数组的方式。您可能会实现自己的打印函数,但它不会显示它们。
#1
32
This is default behavior in jQuery 1.4+...if you want the post to be &stuffs=a&stuffs=b
instead of &stuffs[]=a&stuffs[]=b
you should set the traditional
option to true
, like this:
这是jQuery 1.4+的默认行为……如果你想让邮件为&stuff =a&stuff =b而不是&stuff []=a&stuff []=b,你应该将传统选项设置为true,如下图所示:
$.ajaxSetup({traditional: true});
Note this affects all requests... which is usually what you want in this case. If you want it to be per-request you should use the longer $.ajax()
call and set traditional: true
there. You can find more info about traditional
in the $.param()
documentation.
注意这会影响到所有的请求……这通常是你想要的。如果您希望它是针对每个请求的,您应该使用较长的$.ajax()调用并设置传统的:true。您可以在$.param()文档中找到有关传统的更多信息。
#2
-1
When an array is submitted using a GET request, through a form or AJAX, each element is given the name of the array, followed by a pair of optionally empty square brackets. So the jQuery is generating the url http://example.com/get.php?stuff[]=a&stuff[]=b
. This is the only way of submitting an array, and the javascript is following the standard.
当使用GET请求(通过表单或AJAX)提交数组时,每个元素都被赋予数组的名称,后跟一对可选的空方括号。jQuery生成url http://example.com/get.php?stuff[]=a&stuff[]=b。这是提交数组的唯一方式,javascript遵循标准。
POST
requests work in exactly the same way (unless the json is sent as one long json string).
POST请求的工作方式完全相同(除非json作为一个长json字符串发送)。
In PHP, this is parsed back into the original array, so although the query string can be a little strange, the data is recieved as it was sent. $_GET['stuff'][0]
works correctly in PHP.
在PHP中,这将被解析回原始数组中,因此尽管查询字符串可能有点奇怪,但在发送数据时数据将被接收。$_GET['stuff'][0]在PHP中工作正确。
I'm not sure how Django parses query strings.
我不确定Django如何解析查询字符串。
#3
-3
The []
indicates that the variable is an array. I imagine that the appending of the []
to your variable name is Python/Django's way of telling you it is an array. You could probably implement your own print function which does not show them.
[]表示该变量是一个数组。我认为在变量名后面加上[]是Python/Django告诉您它是一个数组的方式。您可能会实现自己的打印函数,但它不会显示它们。