admin管理员组文章数量:1399006
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
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
Share Improve this question asked Oct 3, 2016 at 15:52 looneytuneslooneytunes 7314 gold badges17 silver badges37 bronze badges 1-
jquery.serialize()
– Marc B Commented Oct 3, 2016 at 15:55
2 Answers
Reset to default 6var 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);
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
本文标签: restHow to pass an array of id39s as parameters for a delete request in javascriptStack Overflow
版权声明:本文标题:rest - How to pass an array of id's as parameters for a delete request in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744142213a2592670.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论