0

When I'm trying to append an id to the end of my query string, the JS adds unnecessary ampersand and '=' sign to the query string.

I'm trying to go for something like this:

http://sample_site/report/file/list?f%5B%5D=1111

but I get this when I view the result in the console:

http://sample_site/report/file/list?f%5B%5D=&f=1111

Here is my JS function that builds a URL object:

buildTileFilter(){
 let url = new URL('http://sample_site/report/file/list?f%5B%5D');
 let query_string = url.search;
 let search_params = new URLSearchParams(query_string);
 search_params.set('f', 1111);
 url.search = search_params.toString();
 let new_url = url.toString();
 return new_url;
 }
asked Mar 15, 2019 at 21:15

1 Answer 1

3

The "%5B%5D" part is being treated as part of the parameter name. You have to add it to the param name that you're setting to get the result you want. That's the encoded value for the string "[]", so to get your result, the code should be:

buildTileFilter(){
 let url = new URL('http://sample_site/report/file/list?f%5B%5D');
 let query_string = url.search;
 let search_params = new URLSearchParams(query_string);
 search_params.set('f[]', 1111);
 url.search = search_params.toString();
 let new_url = url.toString();
 return new_url;
 }
answered Mar 15, 2019 at 21:24

2 Comments

Thanks @JasonR ! I appreciate the quick response! I voted for this answer but since I'm new it's not going to display publicly.
@User1990 No problem!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.