0

Apex Controller

String abc = 'Roger\'O Silva';
        abc = EncodingUtil.urlEncode(abc, 'UTF-8');

Lightning Component

var name = decodeURIComponent(abc);

output from lightning component

apace Space is replaced with + symbol

1
  • why dont you encode/decode on the client? otherwise, you can use the .replace() method
    – glls
    Mar 17, 2020 at 14:25

2 Answers 2

1

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);
0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .