解决Ajax乱码.

1.前台代码

 

 

<script type="text/javascript">
var xmlHttp;
function getCN()
{
 var author = document.forms[0].u367766562624002.value;
 author=encodeURI(author); 
 author=encodeURI(author); //注意:传送的值,2次转码。
 try
    {
   // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
 catch (e)
    {

   //Internet Explorer
   try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
   catch (e)
      {

      try
         {
         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
      catch (e)
         {
         alert("您的浏览器不支持AJAX!");
         return false;
         }
      }
    }
	
    xmlHttp.onreadystatechange=callback;	
	var url = "xzShow.do?show=getcn&uname="+author;
    xmlHttp.open("POST",url,true);
    xmlHttp.send(null);	
 }

 //回调
 function callback(){
	if(xmlHttp.readyState==4){
	   if(xmlHttp.status==200){
		  var result=xmlHttp.responseText;
		  document.forms[0].u367763762460602.value=result;
	   }
	}
   
 }

</script>
 

 2.后台代码

 

	public ActionForward execute(ActionMapping mapping, ActionForm actionform,
			HttpServletRequest request, HttpServletResponse response,
			SessionUser sessionuser) throws Exception {
		//wjb 2011-12-29 下午10:12:42
		 response.setContentType("text/html");
		 response.setCharacterEncoding("GBK");
		 PrintWriter out = response.getWriter();
		 String uname=StringUtils.trim(request.getParameter("uname"));		
		 uname=URLDecoder.decode(uname,"utf8");//注意:解码
		 System.out.println("-----"+uname);
		 if(!uname.equals("")){
			out.print("你好"); 
		 }
		 out.flush();
		 out.close();
		return null;
	}


 

你可能感兴趣的:(JavaScript,java,Ajax,浏览器)