简体   繁体   中英

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. I am sending this data after executing javascript escape() over the data as my content can also contains URL's.

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

I tried following in my javascript code:

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.

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.

<%@ 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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