0

I have a certain text I am encoding in JS using encodeURIComponent. The original text is

weoowuyteeeee !_.

Test could you please resubmit again? 

I am doing the following in my JS code before sending it.

var text = encodeURIComponent($("#txt11").val());

Should I not be doing that?

Once I encode it using encodeURIComponent, it becomes


weoowuyteeeee%2520!_.%252C%250A%250ATest%252C%2520%2520could%2520you%2520please%2520resubmit%2520again%253F


I'm trying to decrypt the same on the Java side using

String decodedString1 = URLDecoder.decode(myObject.getText(), "UTF-8");

but I see this as the output, not the original text. What am I doing wrong?


weoowuyteeeee%20!_.%2C%0A%0ATest%2C%20%20could%20you%20please%20resubmit%20again%3F


3
  • 1
    You're double-encoding it. Don't do that. Commented Apr 10, 2017 at 19:52
  • Updated the post. I am manually doing a encodeURIComponent. Should I not be doing that? Commented Apr 10, 2017 at 20:16
  • That completely depends on how your value is flowing. But it is somehow being encoded twice Commented Apr 10, 2017 at 20:27

1 Answer 1

3

You are encoding your data twice.

Initially, you have encoded your data and later it is encoded again.

Eg: Let your text be

Hello World

After encoding it becomes

Hello%20World

If you encode again it becomes

Hello%2520World

Reason

% from %20 is encoded to %25. So the space becomes %2520.

Normal AJAX can will automatically encode your data before sending to the server side. Check where the 2nd encoding is happening.

Sign up to request clarification or add additional context in comments.

3 Comments

Updated the post. I am manually doing a encodeURIComponent. Should I not be doing that?
Actually it doesn't seem like i'm double encoding. I am only encoding once in JS and decoding in Java.
You can clearly see that the data encoded is double encoded

Your Answer

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.