admin管理员组文章数量:1245032
I am having an issue URL decoding a UTF-8 string in Java that is encoded either with Javascript or Actionscript 3. I've set up a test case as follows:
The string in question is Produktgröße
When I encode with JS/AS3 I get the following string:
escape('Produktgröße')
Produktgr%F6%DFe
When I unescape this with JS I get no change
unescape('Produktgr%F6%DFe')
Produktgr%F6%DFe
So, by this I assume that JS isn't encoding the string properly??
The following JSP produces this outupt
<%@page import="java.URLEncoder"%>
<%@page import="java.URLDecoder"%>
<%=(URLDecoder.decode("Produktgr%F6%DFe","UTF-8"))%><br/>
<%=(URLEncoder.encode("Produktgröße","UTF-8"))%><br/>
<%=(URLEncoder.encode("Produktgröße"))%><br/>
<%=(URLDecoder.decode(URLEncoder.encode("Produktgröße")))%><br/>
<%=(URLDecoder.decode(URLEncoder.encode("Produktgröße"),"UTF-8"))%><br/>
Produktgr?e
Produktgr%C3%B6%C3%9Fe
Produktgr%C3%B6%C3%9Fe
Produktgröße
Produktgröße
Any idea why I'm having this disparity with the languages and why JS/AS3 isn't behaving as I expect it to?
Thanks.
I am having an issue URL decoding a UTF-8 string in Java that is encoded either with Javascript or Actionscript 3. I've set up a test case as follows:
The string in question is Produktgröße
When I encode with JS/AS3 I get the following string:
escape('Produktgröße')
Produktgr%F6%DFe
When I unescape this with JS I get no change
unescape('Produktgr%F6%DFe')
Produktgr%F6%DFe
So, by this I assume that JS isn't encoding the string properly??
The following JSP produces this outupt
<%@page import="java.URLEncoder"%>
<%@page import="java.URLDecoder"%>
<%=(URLDecoder.decode("Produktgr%F6%DFe","UTF-8"))%><br/>
<%=(URLEncoder.encode("Produktgröße","UTF-8"))%><br/>
<%=(URLEncoder.encode("Produktgröße"))%><br/>
<%=(URLDecoder.decode(URLEncoder.encode("Produktgröße")))%><br/>
<%=(URLDecoder.decode(URLEncoder.encode("Produktgröße"),"UTF-8"))%><br/>
Produktgr?e
Produktgr%C3%B6%C3%9Fe
Produktgr%C3%B6%C3%9Fe
Produktgröße
Produktgröße
Any idea why I'm having this disparity with the languages and why JS/AS3 isn't behaving as I expect it to?
Thanks.
Share Improve this question edited May 25, 2011 at 22:47 mrk 5,1273 gold badges28 silver badges42 bronze badges asked May 25, 2011 at 22:02 user710437user710437 551 gold badge1 silver badge5 bronze badges3 Answers
Reset to default 10escape is a deprecated function and does not correctly encode Unicode characters. Use encodeURI or encodeURIComponent, the latter probably being the method most suitable for your needs.
Javascript is URL encoding your string using Latin-1 charset. Java is URL encoding it using UTF-8.
The URL encoding is really just replacing the characters/bytes that it doesn't recognise. For example, even if you were to stick with ASCII characters, (
would be encoded as %28
. You have the additional problem of character sets when you start using non-ASCII characters (any thing longer than 7 bits).
I have been struggling with this problem for hours on end... My problem was a JQuery Ajax call like:
return $.ajax({
url: '/author!getAuthorContent.action',
type: 'GET',
data : {author:name, 'content_type': ct || 'all', 'start': start || 0}
});
'name' is a String which contains special characters like Jérôme-Serrano
For some reasons the way JS/JQuery was encoding these kind of special characters was inpatible and I couldn't decode it on Java BackEnd...
The solution was:
- Encode on JS side using
var econded = encodeURIComponent(name);
- Decode them on Java side using
String decoded = java.URLDecoder.decode(econded ,"UTF-8");
some refetences: http://www.programering./a/MjN2ADOwATg.html http://www.theerrormessage./2013/10/weird-characters-transmitted-to-and-from-server-through-jquery-ajax-call/
本文标签: javascriptDifference in URL decodeencode UTF8 between Java and JSAS3 (bug)Stack Overflow
版权声明:本文标题:javascript - Difference in URL decodeencode UTF-8 between Java and JSAS3 (bug!?) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740149395a2232373.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论