[英]How to send an empty array to PHP server using Javascript
I need to receive a PHP empty array via a form-data
request. 我需要通过
form-data
请求接收PHP空数组。 The notation expected from the body by the PHP server is this: filters[]
. PHP服务器从主体期望的表示法是:
filters[]
。 But when I send an empty array from JS I receive this filters: ""
. 但是,当我从JS发送空数组时,会收到以下
filters: ""
。
My sending method: 我的发送方法:
var filters = new Array();
var body = "filters="+filters;
request.send(body);
So, there's a way to acomplish that naturally in Javascript? 那么,有没有一种方法可以在Javascript中自然完成?
Small change could work for you 零钱可能对您有用
var filters = new Array();
var body = "filters[]="+filters; //added [] to filters
request.send(body);
The URL Parameters should look like this: URL参数应如下所示:
http://example.com/?filter[]=1&filter[]=2
Or 要么
http://example.com/?filter[0]=1&filter[1]=2
etc 等等
As an alternative you could JSON encode a parameter, but then using POST parameters would be the better choice. 或者,您可以对参数进行JSON编码,但是使用POST参数将是更好的选择。
I solved this by sending with the given notation: 我通过发送给定的符号解决了这个问题:
if (!filters || filters.length === 0) {
filters = [""];
}
When the filters are empty. 过滤器为空时。 And an object when the filters are not empty.
和一个对象,当过滤器不为空时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.