You can't simply pass an array as a query parameter. You will need to iterate over the array and add it to the URL string such as ?array[]=one&array[]=two
const a = ['one', 'two']
let url = 'www.google.com'
for (let i = 0; i < a.length; i++) {
// Check to see if the URL has a query string already
if (url.indexOf('?') === -1) {
url = `${url}?array[]=${a[i]}`
} else {
url = `${url}&array[]=${a[i]}`
}
}
console.log(url)
Answer from Cjmarkham on Stack OverflowAnother option using Array.prototype.join
var languages = [2,4,6,8]
lang_params = 'languages='+languages.join('&languages=')
console.log(lang_params)
(as per @Quentin and @Amit comments) If you work with other params that are not integer, you might need to encode the values so they don't interfere with special symbols (i.e. &,?). In such case, you can use encodeURI
lang_params = languages.map((l)=>'languages='+encodeURIComponent(l)).join('&')
The URISearchParams API will handle this case cleanly.
It will also do any escaping of the data that is required to make the values or keys URL safe (in your particular example this doesn't matter, but it is worth paying attention to for the general case).
// Create an object to describe the data in the query string
var query = new URLSearchParams();
// Add the languages
var languages = [1,2,3,4,5]
languages.forEach(function (language) {
query.append("language", language);
});
// Explicitly convert to a string
console.log(query.toString());
Note there is limited browser support at present, so you will probably wish to use a polyfill.
Sending Array Of Objects in GET request (axios)
java - Send an Array with an HTTP Get - Stack Overflow
javascript - How to pass an array through $.get? - Stack Overflow
javascript - How to send an array using GET request from a input form in PHP? - Stack Overflow
I'm implementing filter, sorting, pagination and search functionality via API call. For filter values i have to send array of objects in parameters.
Now I know when we want to send complex params in query we use POST method. But still wants to understand.
In my application, swagger stringify does it without any issue. I can't get the same format which is as below.
e g. This is i want in request headers (payload)
filter:[{name:"descriptive", value: "test"}]
I have tried qs package and JSON.stringify
qs.stringify(params)
or with arrayFormat but not getting the above format in payload.
Instead I'm getting this, which is not acceptable
filter[0][name]: descriptive
filter[0][value]: test
any help would be appreciated.
I know this post is really old, but I have to reply because although BalusC's answer is marked as correct, it's not completely correct.
You have to write the query adding "[]" to foo like this:
foo[]=val1&foo[]=val2&foo[]=val3
That depends on what the target server accepts. There is no definitive standard for this. See also a.o. Wikipedia: Query string:
While there is no definitive standard, most web frameworks allow multiple values to be associated with a single field (e.g.
field1=value1&field1=value2&field2=value3).[4][5]
Generally, when the target server uses a strong typed programming language like Java (Servlet), then you can just send them as multiple parameters with the same name. The API usually offers a dedicated method to obtain multiple parameter values as an array.
foo=value1&foo=value2&foo=value3
String[] foo = request.getParameterValues("foo"); // [value1, value2, value3]
The request.getParameter("foo") will also work on it, but it'll return only the first value.
String foo = request.getParameter("foo"); // value1
And, when the target server uses a weak typed language like PHP or RoR, then you need to suffix the parameter name with braces [] in order to trigger the language to return an array of values instead of a single value.
foo[]=value1&foo[]=value2&foo[]=value3
_GET["foo"]; // [value1, value2, value3]
echo is_array($foo); // true
In case you still use foo=value1&foo=value2&foo=value3, then it'll return only the first value.
_GET["foo"]; // value1
echo is_array($foo); // false
Do note that when you send foo[]=value1&foo[]=value2&foo[]=value3 to a Java Servlet, then you can still obtain them, but you'd need to use the exact parameter name including the braces.
String[] foo = request.getParameterValues("foo[]"); // [value1, value2, value3]
Serialize it with JSON.stringify()?
social_buttons_array : JSON.stringify(social_buttons_array)
A GET request places your values in the URL in the form:
page.php?arg1=value&arg2=value2
So you cannot just pass an associative array unless you somehow convert it to a string value (maybe in Json format, as antisanity suggested).
Another option may be to pass each key of the dictionary as a URL parameter.
var urlParams = {
action: 'process_data',
get: 'social_popup',
};
for (key in social_buttons_array) {
urlParams[key] = social_buttons_array[key];
}
$.get(ajaxurl, urlParams, function(data) {
$('.result').html(data);
});
Which will send something like this:
page.php?action=process_data&get=social_popup&google=0&twitter=0&twitter_follow=0&facebook=0
It really depends how you are going to process that data on the server side.
One option is using a JSON format.
http://server/url?array=["foo","bar"]
Server side
var arr = JSON.parse(req.query.array);
Or your own format
http://server/url?array=foo,bar
Server side
var arr = req.query.array.split(',');
Express exposes the query parameter as an array when it is repeated more than once in the request URL:
app.get('/', function(req, res, next) {
console.log(req.query.a)
res.send(200)
}
GET /?a=x&a=y&a=z:
// query.a is ['x', 'y', 'z']
Same applies for req.body in other methods.
You can send array as a string:
...
data: { categories: JSON.stringify(categories) },
...
GET request does not have message body, so you can't use GET with contentType: 'application/json; charset=UTF-8'. When you use json in GET request, browser breaks your json and append each josn key value to the url(even if you use JSON.stringify method). Therefore for using json and REST you need to use POST method.