Ajax通过POST方法发送中文信息乱码解决

前端之家收集整理的这篇文章主要介绍了Ajax通过POST方法发送中文信息乱码解决前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

开发项目用Ajax的Post方法提交信息到请求的servlet,输出遇到中文乱码,纠结了好几天,总结了网络上很多高手的心得,用了两步解决了乱码:

  1. 在JS端,对提交的字符串进行两次编码:
    Js代码
    1. <%@pagecontentType="text/html"pageEncoding="gbk"%>
    2. <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"
    3. "http://www.w3.org/TR/html4/loose.dtd">
    4. <html>
    5. <head>
    6. <Metahttp-equiv="Content-Type"content="text/html;charset=gbk">
    7. <title>JSPPage</title>
    8. <scripttype="text/javascript">
    9. varreq;
    10. functionvalidate(){
    11. //获取表单提交的内容
    12. varidField=document.getElementById("userName").value;
    13. //要发送的字符串数据
    14. varcontent="id=hello&message="+idField+"&say=我是帅哥";
    15. //重点:对content含中文字符的字符串进行两次编码
    16. content=encodeURI(content);
    17. varurl="validate.do";
    18. //创建一个XMLHttpRequest对象req
    19. if(window.XMLHttpRequest){
    20. //IE7,Firefox,Opera支持
    21. req=newXMLHttpRequest();
    22. }elseif(window.ActiveXObject){
    23. //IE5,IE6支持
    24. newActiveXObject("Microsoft.XMLHTTP");
    25. }
    26. req.open("post",url,true);
    27. req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    28. req.onreadystatechange=callback;
    29. //send函数发送请求,参数
    30. req.send(content);
    31. functioncallback(){
    32. //alert(req.readyState);
    33. //alert(req.status);
    34. if(req.readyState==4&&req.status==200){
    35. //alert(req.responseText);
    36. varcheck=req.responseText;
    37. show(check);
    38. functionshow(str){
    39. document.getElementById("info").innerHTML=str;
    40. </script>
    41. </head>
    42. <body>
    43. <h1>TestAjax</h1><p>
    44. <formaction=""method="get"name="form">
    45. <br>
    46. 输入用户名
    47. <inputtype="text"size="10"maxlength="8"id="userName"name="name"onblur="validate()">
    48. <spanid="info"></span>
    49. 输入商品名:
    50. <inputtype="submit"name="refer">
    51. </form>
    52. </body>
    53. </html>
  2. 在servlet端,对接收的数据的进行一次解码:
    Java代码
      packagecom.model;
    1. importjava.io.IOException;
    2. importjava.io.PrintWriter;
    3. importjava.util.Enumeration;
    4. importjavax.servlet.ServletException;
    5. importjavax.servlet.http.HttpServlet;
    6. importjavax.servlet.http.HttpServletRequest;
    7. importjavax.servlet.http.HttpServletResponse;
    8. importjava.net.URLDecoder;
    9. /**
    10. *
    11. *@authorGavin
    12. */
    13. publicclassDoAjaxServletextendsHttpServlet{
    14. protectedvoidprocessRequest(HttpServletRequestrequest,HttpServletResponseresponse)
    15. throwsServletException,IOException{
    16. response.setContentType("text/html;charset=gbk");
    17. request.setCharacterEncoding("gbk");
    18. PrintWriterout=response.getWriter();
    19. try{
    20. response.setHeader("Cache-Control","no-store");
    21. response.setHeader("Pragma","no-cache");
    22. response.setDateHeader("Expires",0);
    23. //这里有个小问题:如果写成Stringput=null;将会输出一个null后面再加上增加的字符
    24. Stringput="";
    25. for(Enumeration<String>e=request.getParameterNames();e.hasMoreElements();){
    26. Stringh=(String)e.nextElement();
    27. Stringc=(String)request.getParameter(h);
    28. put+=c;
    29. //重点在这里,对接受到的信息进行解码
    30. StringputDecoded=java.net.URLDecoder.decode(put,"utf-8");
    31. System.out.println(pute);
    32. out.write("messageis:"+putDecoded);
    33. finally{
    34. out.close();
    35. @Override
    36. voiddoGet(HttpServletRequestrequest,250); line-height:18px"> processRequest(request,response);
    37. voiddoPost(HttpServletRequestrequest,85); font-weight:bold">publicStringgetServletInfo(){
    38. return"Shortdescription";
    39. }//</editor-fold>
    40. }
原文链接:https://www.f2er.com/ajax/165521.html

猜你在找的Ajax相关文章