0

I have an array of id's whihc i want to append to a delete request. How should i append the params to the url request?

var deleteArray = ['1', '5', '6'];

if i want to send a request in this format http://localhost/xxx/employees?ids=1&ids=5&ids=6 for delete

How do i parse the array contents to build the ids=1&ids=5&ids=6 for the request url for delete?

My concern is the "&" , how could i append "& and build the string for the url request in javascript

looneytunes
  • 741
  • 4
  • 16
  • 35

2 Answers2

6
var deleteArray = ['1', '5', '6'];

You need key=value pairs so…

var pairs = deleteArray.map(function (value) { return "id=" + encodeURIComponent(value) });

Then you need them joined by ampersands so:

var query_string = pairs.join("&");

var deleteArray = ['1', '5', '6'];
var pairs = deleteArray.map(function (value) { return "id=" + encodeURIComponent(value) });
var query_string = pairs.join("&");
console.log(query_string);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

To pass the array in the Get you should create ids[]=1&ids[]=2 etc

Here is how you will create the GET URL

var url = 'http://localhost/xxx/employees?ids[]=' + deleteArray.join('&ids[]=');

This will create URL like

 http://localhost/xxx/employees?ids[]=1&ids[]=5&ids[]=6
Nadeem Manzoor
  • 760
  • 5
  • 14