I'm trying to use jquery.post to pass a parameter items
with an Array
as its value to the server. Like this:
我正在尝试使用jquery.post将带有Array作为其值的参数项传递给服务器。喜欢这个:
HTML:
总共 <span class="statistics" data-fields="Record_entries_count"></span> 条记录,
总共 <span class"statistics" data-fields="drop_count"></span> 次。
Javascript:
this.items = this.element.find('span.statistics').toArray().map(i=>$(i).data('fields'));
$.post('backend_server', {"items": this.items}, function(data, status, xhr){...});
However, strangely enough it doesn't get passed correctly. I used the developer's tool to check the request header and found that what I passed as items
becomes items[]
.
但是,奇怪的是它没有正确传递。我使用开发人员的工具检查请求标头,发现我作为项目传递的内容变为items []。
Why is this? Could someone please give a hint? Thanks a lot.
为什么是这样?有人可以提一下吗?非常感谢。
1 个解决方案
#1
2
As this.items
is an array, jQuery will introduce these brackets, as if you had done:
由于this.items是一个数组,jQuery将引入这些括号,就好像你已经完成了:
$.post('backend_server', {"items[]": this.items}, ....);
You can do it like that yourself, but jQuery will do this for you when you don't. See the examples in the jQuery documentation.
你可以自己这样做,但是当你不这样做时,jQuery会为你做这件事。请参阅jQuery文档中的示例。
This is needed because the data structure that a post
request supports is by nature flat. It follows the HTTP specification for "application/x-www-form-urlencoded", i.e. one or more parameter = value
pairs in URL encoding. See this Q&A for several interesting answers on that format.
这是必需的,因为post请求支持的数据结构本质上是平的。它遵循“application / x-www-form-urlencoded”的HTTP规范,即URL编码中的一个或多个参数=值对。有关该格式的几个有趣答案,请参阅此问答。
There is a consensus that parameter names with added square brackets denote that the original data is an array. So every array value will be encoded as a value of an items[]
parameter.
人们一致认为,带有方括号的参数名称表示原始数据是一个数组。因此,每个数组值都将编码为items []参数的值。
Several server applications support this notation and will interpret it again as an array (e.g. PHP does this). If not, you'll have to take care of this at the server side.
一些服务器应用程序支持这种表示法,并将它再次解释为一个数组(例如PHP这样做)。如果没有,你将不得不在服务器端处理这个问题。
Alternatives
If you want jQuery not to modify the names like that, you could change the jQuery settings for this with jQuery.ajaxSettings.traditional = true;
.
如果你想让jQuery不修改这样的名字,你可以用jQuery.ajaxSettings.traditional = true;来改变它的jQuery设置。
Alternatively, you could stringify your object as JSON and send that string as one value:
或者,您可以将对象字符串化为JSON并将该字符串作为一个值发送:
$.post('backend_server', {"items": JSON.stringify(this.items) }, ....);
At the server side you'll then have to parse that JSON.
在服务器端,您将必须解析该JSON。
#1
2
As this.items
is an array, jQuery will introduce these brackets, as if you had done:
由于this.items是一个数组,jQuery将引入这些括号,就好像你已经完成了:
$.post('backend_server', {"items[]": this.items}, ....);
You can do it like that yourself, but jQuery will do this for you when you don't. See the examples in the jQuery documentation.
你可以自己这样做,但是当你不这样做时,jQuery会为你做这件事。请参阅jQuery文档中的示例。
This is needed because the data structure that a post
request supports is by nature flat. It follows the HTTP specification for "application/x-www-form-urlencoded", i.e. one or more parameter = value
pairs in URL encoding. See this Q&A for several interesting answers on that format.
这是必需的,因为post请求支持的数据结构本质上是平的。它遵循“application / x-www-form-urlencoded”的HTTP规范,即URL编码中的一个或多个参数=值对。有关该格式的几个有趣答案,请参阅此问答。
There is a consensus that parameter names with added square brackets denote that the original data is an array. So every array value will be encoded as a value of an items[]
parameter.
人们一致认为,带有方括号的参数名称表示原始数据是一个数组。因此,每个数组值都将编码为items []参数的值。
Several server applications support this notation and will interpret it again as an array (e.g. PHP does this). If not, you'll have to take care of this at the server side.
一些服务器应用程序支持这种表示法,并将它再次解释为一个数组(例如PHP这样做)。如果没有,你将不得不在服务器端处理这个问题。
Alternatives
If you want jQuery not to modify the names like that, you could change the jQuery settings for this with jQuery.ajaxSettings.traditional = true;
.
如果你想让jQuery不修改这样的名字,你可以用jQuery.ajaxSettings.traditional = true;来改变它的jQuery设置。
Alternatively, you could stringify your object as JSON and send that string as one value:
或者,您可以将对象字符串化为JSON并将该字符串作为一个值发送:
$.post('backend_server', {"items": JSON.stringify(this.items) }, ....);
At the server side you'll then have to parse that JSON.
在服务器端,您将必须解析该JSON。