pass array as parameter in an axios request
15,626
You can POST
it as json data
let data=[1,2,3,4,5];
let json=JSON.stringify(data);
let post_data={json_data:json}
axios.post('/url',post_data)
- use
JSON.stringify
to convert it to json string - Use POST method to send data to server
- Use json_decode to convert json back to array on server side
On laravel side you can do like below
$jsonArray = json_decode($response,true);
Author by
FeRcHo
Updated on June 14, 2022Comments
-
FeRcHo almost 2 years
I need to make a request through axios, in which I want to pass as an array an array of this type
[1,2,3,4]
. I need this data to make a selection query from my backend, my question is: should I use aGET
orPOST
request and what would be the correct way to pass this array? -
FeRcHo about 6 yearsthanks for answering, apparently everything is fine with my javascript code, but I have problems receiving the array in my laravel driver, apparently this did not work $ jsonArray = json_decode ($ response, true).
-
Kevin Foster over 4 yearsFor best practices, the answer would depend on the purpose of your request, and you shouldn't use get/post interchangeably. Get requests should be idempotent, while post requests are not: restapitutorial.com/lessons/idempotency.html