简体   繁体   中英

Encoding/Escaping Spaces for URL GET Request JavaScript

I'm currently trying to handle a bug in my system where my GET requests to query my database cannot handle spaces. My GET request in JavaScript looks like so and I am 100% sure works for cases without spaces:

let values = JSON.parse(ajax().sync().get(url));
             

For an example of the bug, take the following URL:

localhost:8000/database/data?id_one=one&id_two=one hundred

Obviously, passing that in returns nothing because I'm not accounting for the space. So I then try to escape the space with '%20' like so:

localhost:8000/database/data?id_one=one&id_two=one%20hundred

However, this is still returning nothing, as I can see on the server-side that it is passing in the URL with '%20' as a literal into the query; it's querying the database for 'one%20hundred' and not 'one hundred'. I can tell becasue the 'GET' on the server side looks like this:

GET localhost:8000/database/data?id_one=one&id_two=one%20hundred 304 1.877 ms

How do I escape a space like this to query the database properly? I've tried using '+' in place of '%20' but that's not solving it either unfortunately.

Have you considered deconstructing the request server side? I assume you're using Node.JS with Express. If this is the case try to remove the special characters (+ and %20) in the req.param.id_two .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM