Re: Ajax can't receive Chinese code from action of struts2
On 4/28/07, red phoenix <[EMAIL PROTECTED]> wrote: when ajax receive information which include Chinese code from action of Struts2,ajax will show chinese code into confusion code,but if ajax receive information which include Chinese code from jsp, ajax will show correct Chinese code.I don't know why raise this error! How to correct it? response.setContentType("text/html;charset=gb2312"); You set the charset to gb2312... response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pargma","no-cache"); (Note: that should read "Pragma", not "Pargma".) StringBuffer sb=new StringBuffer(""); sb.append ("北京yangzhousuzhou"); sb.append(""); PrintWriter out=response.getWriter(); out.write(sb.toString()); But you don't send gb2312 bytes back to the client. Java strings are Unicode (internally, UTF-16be), and when you convert them to bytes without specifying what encoding to use, you get the "default platform encoding", which could be just about anything, depending on what platform you are on, the JVM and appserver you are using, the version, the phase of the moon, etc. If you want to convert the string to gb2312 bytes, you need to convert the string to gb2312 bytes. out.write(sb.toString().getBytes("gb2312")); N.B. this can throw an UnsupportedEncodingException, in theory, although it shouldn't for well-known encodings, in practice. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Ajax can't receive Chinese code from action of struts2
Maybe you should url-encode them first before sending them to client through ajax response. 2007/4/28, Dave Newton <[EMAIL PROTECTED]>: --- red phoenix <[EMAIL PROTECTED]> wrote: > when ajax receive information which include Chinese > code from action of Struts2,ajax will show chinese > code into confusion code,but if ajax receive > information which include Chinese code from jsp, > ajax will show correct Chinese code.I don't know why > raise this error! How to correct it? I believe that in order to compile I18N code you must do two things: make sure your source is in an I18N format and tell the compiler to use that encoding. I think with Sun's compiler you can use the -encoding option. d. __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- Wong Tseng 王曾
Re: Ajax can't receive Chinese code from action of struts2
--- red phoenix <[EMAIL PROTECTED]> wrote: > when ajax receive information which include Chinese > code from action of Struts2,ajax will show chinese > code into confusion code,but if ajax receive > information which include Chinese code from jsp, > ajax will show correct Chinese code.I don't know why > raise this error! How to correct it? I believe that in order to compile I18N code you must do two things: make sure your source is in an I18N format and tell the compiler to use that encoding. I think with Sun's compiler you can use the -encoding option. d. __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Ajax can't receive Chinese code from action of struts2
when ajax receive information which include Chinese code from action of Struts2,ajax will show chinese code into confusion code,but if ajax receive information which include Chinese code from jsp, ajax will show correct Chinese code.I don't know why raise this error! How to correct it? My code is follows: /*a.html saved as UTF-8 code*/ var xmlhttp; function CreateXmlRequest(){ try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e){ try{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ try{ xmlhttp = new XMLHttpRequest(); } catch (e){ xmlhttp = false; } } } } function checkUserName(){ xmlhttp.open("get", "example/test.action", true); xmlhttp.onreadystatechange = updatePage; xmlhttp.send(null); } function updatePage(){ try{ if(xmlhttp.readyState == 4){ alert(xmlhttp.responseText); } } catch (e){} } CreateXmlRequest(); /*HelloWorld.java saved as UTF-8 no BOM code,I compile as C:>javac -d . HelloWorld.java */ package example; import org.apache.struts2.ServletActionContext; import java.io.*; import javax.servlet.http.HttpServletResponse; public class HelloWorld extends ExampleSupport { public String test() throws Exception { HttpServletResponse response; response=ServletActionContext.getResponse(); response.setContentType("text/html;charset=gb2312"); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pargma","no-cache"); response.setDateHeader("Expires",0); StringBuffer sb=new StringBuffer(""); sb.append ("北京yangzhousuzhou"); sb.append(""); PrintWriter out=response.getWriter(); out.write(sb.toString()); out.close(); return null; } } /*struts.xml*/ http://struts.apache.org/dtds/struts-2.0.dtd";> /*example.xml*/ http://struts.apache.org/dtds/struts-2.0.dtd";> /example/a.jsp When I run a.html,ajax should alert information "北京yangzhousuzhou",but it shows "?confusion characters?yangzhousuzhou" Anybody know how to do it? Thanks!