javascript转义,encodeURI和encodeURIComponent的问题

时间:2021-05-24 18:24:02

My application sends data from javascript to my servlet using ajax POST request and that data contains special characters as well. I am sending this data after executing javascript escape() over the data as my content can also contains URL's.

我的应用程序使用ajax POST请求将数据从javascript发送到我的servlet,并且该数据也包含特殊字符。我在对数据执行javascript escape()之后发送此数据,因为我的内容也可以包含URL。

The problem is that I do not get correct data at my servlet end and I am suspecting that escape is creating the issue.

问题是我的servlet端没有得到正确的数据,我怀疑逃避是否会产生问题。

I tried following in my javascript code :

我在我的javascript代码中尝试了以下内容:

alert(escape("»"));
alert(unescape(escape("»")));

alert(encodeURI("»"));
alert(decodeURI(encodeURI("»")));

alert(encodeURIComponent("»"));
alert(decodeURIComponent(encodeURIComponent("»")));

I do not get correct alert when I am unescaping the escape string, As seen by the code, I have also tried using encodeURI, encodeURIComponent and their respective decoding methods.

当我无法转义转义字符串时,我没有得到正确的警告。正如代码所示,我也尝试过使用encodeURI,encodeURIComponent及其各自的解码方法。

How should I send the data from javascript so that all of the special character and other part of the content (like URL's) are received properly.

我应该如何从javascript发送数据,以便正确接收所有特殊字符和内容的其他部分(如URL)。

1 个解决方案

#1


1  

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

was the issue, Changed it to UTF-8, works fine now.

问题是,将其改为UTF-8,现在工作正常。

#1


1  

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

was the issue, Changed it to UTF-8, works fine now.

问题是,将其改为UTF-8,现在工作正常。