I am working on a project using Node.js v21.7.1, from a MariaDB database I query first name and surname then pass these back to the client.
In my SQL I use:
CONCAT(`t2`.`vcSurName`, ',', `t2`.`vcFirstName`) AS vcName
My client receives this and adds it to a 'SELECT' HTML tag, I can see the name is displayed correctly, for example 'Platten, Simon' is shown, this is now part of a form which when submitted, I can see that the server receives the name as:
'Platten%2C+Simon'
On the server I have used:
var strUserName = decodeURIComponent(objFields['biUID']);
I then split the name into an array:
var aryName = strUserName.split(', ');
This is where is goes wrong, decodeURIComponent only translates the %2C back to a comma, but the content of aryName now contains:
[Platten,+Simon]
What do I need to do because the + is the result of encoding the space but it doesn't get decoded...
decodeURIComponent()
only handles the percentage-encoded parts of the string. Decoding the +
character needs to be done separately.
See also the Decoding query parameters from a URL chapter of MDN's decodeURIComponent()
article, where it suggests the following code:
function decodeQueryParam(p) {
return decodeURIComponent(p.replace(/\+/g, " "));
}
decodeQueryParam("search+query%20%28correct%29");
// 'search query (correct)'