admin管理员组文章数量:1341873
I need to make URLs like example?want[]=1,2,3&want[]=1,2,3&want[]=1,2,3
let params = new URLSearchParams();
for(let i in searchArr) {
params.append('want[]', `${searchArr[i].id},${searchArr[i].quality},${searchArr[i].level}`);
}
console.log(params);
I've verified the given values exists, but it's always showing URLSearchParams {}
with no values in console.
Why is it empty?
I need to make URLs like example.?want[]=1,2,3&want[]=1,2,3&want[]=1,2,3
let params = new URLSearchParams();
for(let i in searchArr) {
params.append('want[]', `${searchArr[i].id},${searchArr[i].quality},${searchArr[i].level}`);
}
console.log(params);
I've verified the given values exists, but it's always showing URLSearchParams {}
with no values in console.
Why is it empty?
Share asked Aug 12, 2019 at 19:27 user2722718user2722718 1731 gold badge2 silver badges13 bronze badges 02 Answers
Reset to default 8URLSearchParams is an object. You need to cast it to a String using toString()
, like so:
var params = new URLSearchParams();
params.append('want[]', '1,2,3');
params.append('want[]', '1,2,3');
params.append('want[]', '1,2,3');
console.log(params.toString());
(Note: This will URL-encode characters like "[", "]", and ",")
You can access values by using any of these methods available on URLSearchParams
getAll()
-> For a specific search parameter.entries()
-> For all key/value pairs.values()
-> For all the values
var params = new URLSearchParams();
params.append('want[]', '1,2,3');
params.append('want[]', '1,2,3');
params.append('want[]', '1,2,3');
console.log(params.getAll('want[]'));
console.log([...params.entries()]);
console.log([...params.values()]);
本文标签: javascriptURLSearchParams always returning emptyStack Overflow
版权声明:本文标题:javascript - URLSearchParams always returning empty - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743686365a2521961.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论