PDA

View Full Version : how to post form data of non-English character


ronnin
07-22-2007, 11:17 PM
I'm trying to post form data in AJAX way, in which some fields has value of non-English character.
here is the sample form:

<html>
<head>
<title>sample</title>
<!--charset GB2312 is a charset of Simplified Chinese characters-->
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<link href="ext-1.1-rc1/resources/css/ext-all.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="ext-1.1-rc1/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="ext-1.1-rc1/ext-all-debug.js"></script>
<script type="text/javascript" src="ext-1.1-rc1/source/locale/ext-lang-zh_CN.js" charset="UTF-8"></script>
<script type="text/javascript" language="JavaScript">
Ext.BLANK_IMAGE_URL = "../images/s.gif";
</script>
<script type="text/javascript" src="js/sample.js"></script>
</head>
<body>

<form id="form-sample" name="form-sample" method="post">
<!--field has the value of two Chinese characters.-->
<input type="text" id="fuzz-field" name="fuzz-field" value="中文"/>
</form>

</body>
</html>


and here is part of sample.js, helpless in serval way:

Ext.Ajax.request({
url: "save.jsp",
form: "form-sample",
method: "POST",
headers: {"Content-Type":"application/x-www-form-urlencoded;charset=GB2312"},
success: cmnActionSuccess,
failure: cmnActionFailure
});

// another headers of Request
headers: {"Content-Type":"text/html;charset=GB2312"},

// another headers of Request
headers: {"encoding":"GB2312"},


BTW, this issue might be much common for non-english users,
but I have searched the forum by several keywords, such as "ajax form", "ajax encoding" "ajax charset", "form encoding", "headers", "non-english",etc. there is no helpfull results for me.

Skeleton
07-23-2007, 02:15 AM
Try using UTF-8 for encoding because the forms use the encoding of their parent documents during data transmission. For this, you should use the following meta tag to specify your Content Type:

<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

You should also save your html files in UTF-8 encoding.

sosyxg
07-23-2007, 02:44 AM
in your jsp page:

String Test = new String(request.getParameter("Username").getBytes("ISO-8859-1"),"gb2312");


---------------------------------
QQ:181679118
ICQ:212830745
MSN:sosyxg@hotmail.com
---------------------------------

ronnin
07-23-2007, 04:11 AM
thank you all.

to sosyxg (http://extjs.com/forum/member.php?u=5384):
it dosen't work. another way, either:

java.net.UrlDecoder.decode(request.getParameter("fuzz-field"), "GB2312");
//or
java.net.UrlDecoder.decode(request.getParameter("fuzz-field"), "UTF-8");


to Skeleton: (http://extjs.com/forum/member.php?u=1284)
if the pages(sender,reciever) are in UTF-8, there is no such problem.
in the page of reciever, use java.net.URLDecoder.decode(request.getParameter(key), "UTF-8"); will get the right string.

but for some reason, I need to use GB2312 encoding.

Dose the browers(IE, FireFox) send form data in UTF-8, as default?
if so, I think what i need is a client-side encoding convertion.

jpdiez
11-06-2007, 09:34 PM
Thanks a lot sosyxg. Worked perfectly for me in the Struts Action. :D

in your jsp page:

String Test = new String(request.getParameter("Username").getBytes("ISO-8859-1"),"gb2312");


---------------------------------
QQ:181679118
ICQ:212830745
MSN:sosyxg@hotmail.com
---------------------------------

jackliu.aws
11-25-2007, 12:52 AM
you can write a filter for your J2ee Application

ajaxData=new String(ajaxData.getBytes("GB2312"),"UTF-8");