I’m trying to pass an array to a php script as part of an Ajax call. Whilst POST might be simple, it seems GET is the right technique to do this, I’m also making the api code so it can be either way that I send to it. Therefore I want to put the array contents into the url as 2 separate parameters of the request.
After a bit of reading about loops I found the suggestion of using “spread”: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
However I’m not clear how to use it properly. This was my code:
const arg = ['p0', 'p1'];
callMe(arg);
function callMe(…arg){
// ajax GET request
let url = `api.php?p0=${1}&p1=${2}`;
// rest of code…
console.log(arg);
}
This still prints out as an array. How should I be using spread to get p0, p1 put into a url from an array?