免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1898 | 回复: 1
打印 上一主题 下一主题

解决Ajax乱码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-31 11:20 |只看该作者 |倒序浏览
解决Ajax乱码







1.前台代码





Js代码
  1. 1.<script type="text/javascript">   
  2. 2.var xmlHttp;   
  3. 3.function getCN()   
  4. 4.{   
  5. 5. var author = document.forms[0].u367766562624002.value;   
  6. 6. author=encodeURI(author);   
  7. 7. author=encodeURI(author); //注意:传送的值,2次转码。   
  8. 8. try  
  9. 9.    {   
  10. 10.   // Firefox, Opera 8.0+, Safari   
  11. 11.    xmlHttp=new XMLHttpRequest();   
  12. 12.    }   
  13. 13. catch (e)   
  14. 14.    {   
  15. 15.  
  16. 16.   //Internet Explorer   
  17. 17.   try  
  18. 18.      {   
  19. 19.      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");   
  20. 20.      }   
  21. 21.   catch (e)   
  22. 22.      {   
  23. 23.  
  24. 24.      try  
  25. 25.         {   
  26. 26.         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");   
  27. 27.         }   
  28. 28.      catch (e)   
  29. 29.         {   
  30. 30.         alert("您的浏览器不支持AJAX!");   
  31. 31.         return false;   
  32. 32.         }   
  33. 33.      }   
  34. 34.    }   
  35. 35.      
  36. 36.    xmlHttp.onreadystatechange=callback;      
  37. 37.    var url = "xzShow.do?show=getcn&uname="+author;   
  38. 38.    xmlHttp.open("POST",url,true);   
  39. 39.    xmlHttp.send(null);   
  40. 40. }   
  41. 41.  
  42. 42. //回调   
  43. 43. function callback(){   
  44. 44.    if(xmlHttp.readyState==4){   
  45. 45.       if(xmlHttp.status==200){   
  46. 46.          var result=xmlHttp.responseText;   
  47. 47.          document.forms[0].u367763762460602.value=result;   
  48. 48.       }   
  49. 49.    }   
  50. 50.      
  51. 51. }   
  52. 52.  
  53. 53.</script>  
  54. <script type="text/javascript">
  55. var xmlHttp;
  56. function getCN()
  57. {
  58. var author = document.forms[0].u367766562624002.value;
  59. author=encodeURI(author);
  60. author=encodeURI(author); //注意:传送的值,2次转码。
  61. try
  62.     {
  63.    // Firefox, Opera 8.0+, Safari
  64.     xmlHttp=new XMLHttpRequest();
  65.     }
  66. catch (e)
  67.     {

  68.    //Internet Explorer
  69.    try
  70.       {
  71.       xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  72.       }
  73.    catch (e)
  74.       {

  75.       try
  76.          {
  77.          xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  78.          }
  79.       catch (e)
  80.          {
  81.          alert("您的浏览器不支持AJAX!");
  82.          return false;
  83.          }
  84.       }
  85.     }
  86.        
  87.     xmlHttp.onreadystatechange=callback;       
  88.         var url = "xzShow.do?show=getcn&uname="+author;
  89.     xmlHttp.open("POST",url,true);
  90.     xmlHttp.send(null);       
  91. }

  92. //回调
  93. function callback(){
  94.         if(xmlHttp.readyState==4){
  95.            if(xmlHttp.status==200){
  96.                   var result=xmlHttp.responseText;
  97.                   document.forms[0].u367763762460602.value=result;
  98.            }
  99.         }
  100.    
  101. }

  102. </script>  
复制代码
2.后台代码



Java代码
  1. 1.public ActionForward execute(ActionMapping mapping, ActionForm actionform,   
  2. 2.        HttpServletRequest request, HttpServletResponse response,   
  3. 3.        SessionUser sessionuser) throws Exception {   
  4. 4.    //wjb 2011-12-29 下午10:12:42   
  5. 5.     response.setContentType("text/html");   
  6. 6.     response.setCharacterEncoding("GBK");   
  7. 7.     PrintWriter out = response.getWriter();   
  8. 8.     String uname=StringUtils.trim(request.getParameter("uname"));         
  9. 9.     uname=URLDecoder.decode(uname,"utf8");//注意:解码   
  10. 10.     System.out.println("-----"+uname);   
  11. 11.     if(!uname.equals("")){   
  12. 12.        out.print("你好");   
  13. 13.     }   
  14. 14.     out.flush();   
  15. 15.     out.close();   
  16. 16.    return null;   
  17. 17.}  
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-01-03 22:09 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP