简体   繁体   English

Javascript encodeURIComponent和Java解码问题

[英]Javascript encodeURIComponent and Java decode issue

I have a certain text I am encoding in JS using encodeURIComponent. 我有某些文本要使用encodeURIComponent在JS中编码。 The original text is 原文是

weoowuyteeeee !_.

Test could you please resubmit again? 

I am doing the following in my JS code before sending it. 发送之前,我正在JS代码中执行以下操作。

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

Should I not be doing that? 我不应该那样做吗?

Once I encode it using encodeURIComponent, it becomes 一旦我使用encodeURIComponent对其进行编码,它就会变成


weoowuyteeeee%2520!_.%252C%250A%250ATest%252C%2520%2520could%2520you%2520please%2520resubmit%2520again%253F 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 我正在尝试使用Java解密相同的内容

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 weoowuyteeeee%20!_。%2C%0A%0ATest%2C%20%20could%20you%20please%20resubmit%20again%3F


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 你好%20World

If you encode again it becomes 如果再次编码,它将变成

Hello%2520World 你好%2520World

Reason 原因

% from %20 is encoded to %25. %20中的%被编码为%25。 So the space becomes %2520. 因此空间变为%2520。

Normal AJAX can will automatically encode your data before sending to the server side. 普通的AJAX可以在发送到服务器端之前自动对您的数据进行编码。 Check where the 2nd encoding is happening. 检查第二编码发生在哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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