469,282 Members | 1,815 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

AJAX explicitly set the encoding when sending out the post request

I am trying to send request using ajax with some japanese text.
As any of you know XMLHttpRequest is send with utf-8.

I was hoping there is a way to send with different encoding like

var parameters = "metadata=" +
encodeURI(document.getElementById("metadata").valu e);

http_request.onreadystatechange = metadataUpdate;
http_request.open( 'POST', "/servlet/SendJapText", true );
http_request.setRequestHeader( "Content-type",
"application/x-www-form-urlencoded;charset=Shift-JIS" );
http_request.send( parameters );

Thanks in advance.

Jul 27 '06 #1
1 6111


sm************@gmail.com wrote:

var parameters = "metadata=" +
encodeURI(document.getElementById("metadata").valu e);
I would suggest to use encodeURIComponent and not encodeURI (as the
value to be encoded could e.g. contain '=' or '&' meaning with encodeURI
you would get with value 'Kibo&Xibo'
metadata=Kibo&Xibo
while with encodeURIComponent you get
metadata=Kibo%26Xibo
).
http_request.onreadystatechange = metadataUpdate;
http_request.open( 'POST', "/servlet/SendJapText", true );
http_request.setRequestHeader( "Content-type",
"application/x-www-form-urlencoded;charset=Shift-JIS" );
http_request.send( parameters );
If you set the Content-Type request header it is up to your code to
ensure the argument you pass to the send method is properly encoded.
Thus if you want charset=Shift-JIS then you can't use encodeURI
respectively encodeURIComponent as those do not use/support Shift-JIS as
the charset, rather UTF-8.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 28 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by NextOne | last post: by
2 posts views Thread by dmagliola | last post: by
1 post views Thread by geevaa | last post: by
2 posts views Thread by Zeba | last post: by
4 posts views Thread by Mike P2 | last post: by
10 posts views Thread by mouac01 | last post: by
11 posts views Thread by =?Utf-8?B?R2VyaGFyZA==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.