Apex Controller
String abc = 'Roger\'O Silva';
abc = EncodingUtil.urlEncode(abc, 'UTF-8');
Lightning Component
var name = decodeURIComponent(abc);
output from lightning component
Apex Controller
String abc = 'Roger\'O Silva';
abc = EncodingUtil.urlEncode(abc, 'UTF-8');
Lightning Component
var name = decodeURIComponent(abc);
output from lightning component
It seems like the content you are encoding is some data and not any URL.
An alternate approach would be to use EncodingUtil.base64Encode(blob.valueof(content)) to encode and atob(encodedContent) in js to get the content back.
Apex
String content = '';
String encodedContent = EncodingUtil.base64Encode(blob.valueof(content));
JS
const content = atob(encodedContent);
The issue is that EncodingUtil.urlEncode()
encodes data as application/x-www-form-urlencoded
, which includes replacing space with + (and + with %2B btw).
As you can read at https://stackoverflow.com/questions/1547899/which-characters-make-a-url-invalid a + is a perfectly valid character for an URI. Therefore decoding an URI component which contains a +
will result in a +
. Not so with urlencoded
forms.
If you are only concerned with the space/+ issue, you have to replace that character in your apex controller:
String abc = 'Roger\'O Silva';
abc = EncodingUtil.urlEncode(abc, 'UTF-8');
abc = abc.replace ('+', '%20');
%20
is a perfectly valid URI and URL representation of a space. Then decodeURIComponent()
will show the correct spaces.