AJAX

来源:互联网 发布:制作彩铃用什么软件 编辑:程序博客网 时间:2024/06/05 06:01
  1. <html>   
  2. <body>   
  3. <script type="text/javascript">   
  4. function ajaxFunction()   
  5. {   
  6. var xmlHttp;   
  7. try  
  8.   {   
  9.   // Firefox, Opera 8.0+, Safari   
  10.   xmlHttp=new XMLHttpRequest();   
  11.   }   
  12. catch (e)   
  13.    {   
  14.   // Internet Explorer   
  15.   try  
  16.     {   
  17.     xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");   
  18.     }   
  19.   catch (e)   
  20.     {   
  21.     try  
  22.       {   
  23.       xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");   
  24.       }   
  25.     catch (e)   
  26.       {   
  27.       alert("Your browser does not support AJAX!");   
  28.       return false;   
  29.       }   
  30.     }   
  31.   }   
  32.   xmlHttp.onreadystatechange=function()   
  33.     {   
  34.     if(xmlHttp.readyState==4 && xmlHttp.status == 200)   
  35.       {   
  36.       alert(xmlHttp.responseText)   
  37.      // document.write(xmlHttp.responseText);   
  38.       document.myForm.time.value=xmlHttp.responseText;   
  39.          
  40.       }   
  41.     }   
  42.   
  43.       xmlHttp.open('POST''/myProject/test01.do?method=ajax'true);    
  44. //其中 test01是在struts的配置文件的action 的path属性中设置的   
  45.   xmlHttp.send(null);   
  46.   }   
  47. </script>   
  48.   
  49. <form name="myForm">   
  50. Name: <input type="text"  
  51. onkeyup="ajaxFunction();" name="username" />    
  52. Time: <input type="text" name="time" />   
  53. </form>   
  54. </body>   
  55. </html>  

 

 

 

 

 

  1. public class TestAction extends DispatchAction {       
  2.     public ActionForward ajax(ActionMapping mapping, ActionForm form,   
  3.             HttpServletRequest request, HttpServletResponse response) {   
  4.         for(int i=0;i<5;i++){   
  5.             System.out.println("99999999999999999999999999");   
  6.         }   
  7.   
  8.         response.setHeader("Cache-Control""no-cache");//不缓冲   
  9.         response.setHeader("Pragma""no-cache");//不缓冲   
  10. response.setCharacterEncoding("GBK");//汉字编码   
  11.         PrintWriter out =null;   
  12.         try {   
  13.             out = response.getWriter();   
  14.         } catch (IOException e) {   
  15.             e.printStackTrace();   
  16.         }   
  17.         out.print("ajax test ");   
  18.         return null;   
  19.     }   

原创粉丝点击