简体   繁体   English

javascript 转义、encodeURI 和 encodeURIComponent 的问题

[英]Issue with javascript escape, encodeURI and encodeURIComponent

My application sends data from javascript to my servlet using ajax POST request and that data contains special characters as well.我的应用程序使用 ajax POST 请求将数据从 javascript 发送到我的 servlet,并且该数据也包含特殊字符。 I am sending this data after executing javascript escape() over the data as my content can also contains URL's.我在对数据执行 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 端没有得到正确的数据,我怀疑escape正在造成问题。

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)。

<%@ 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,现在可以正常工作。

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

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