Sometimes, we want to correctly use axios params with JavaScript arrays.
In this article, we’ll look at how to correctly use axios params with JavaScript arrays.
How to correctly use axios params with JavaScript arrays?
To correctly use axios params with JavaScript arrays, we serialize them to a string.
For instance, we write
axios.get("/myController/myAction", {
params: {
storeIds: [1, 2, 3],
},
paramsSerializer: (params) => {
return qs.stringify(params);
},
});
to call get
with the storeIds
params.
We set the paramsSerializer
property to a function that returns the stringified parameters we get from qs.stringify
.
stringify
converts the params
object into a query string with all the parameters encoded.
Conclusion
To correctly use axios params with JavaScript arrays, we serialize them to a string.