zoukankan      html  css  js  c++  java
  • Ajax基础实例

    前端代码 

     <script type="text/javascript">
        var xmlhttp;
        function go(url)
        {
        xmlhttp=null;
        if (window.XMLHttpRequest)
          {// code for IE7, Firefox, Opera, etc.
          xmlhttp=new XMLHttpRequest();
          }
        else if (window.ActiveXObject)
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        ////////////////////////////////
        if (xmlhttp!=null)
          {
            //alert("123");
            xmlhttp.onreadystatechange=state_Change;
            xmlhttp.open("POST",url,true);
            xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xmlhttp.send("123="+"123");
          
          }
        else
          {
          alert("Your browser does not support XMLHTTP.");
          }
        }
    
        function state_Change()
        {
        if (xmlhttp.readyState==4)
          {// 4 = "loaded"
          if (xmlhttp.status==200)
            {// 200 = "OK"
           var v1=xmlhttp.status;
           var v2=xmlhttp.statusText;
           var v3=xmlhttp.responseText; 
           alert(v1+"    "+v2+"   "+v3);
            }
          else
            {
            alert("Problem retrieving XML data:" + xmlhttp.statusText);
            }
          }
        }
        </script>
      <body>
        This is my JSP page. <br>
         
         <input type="text" value="" name="123" />  
        <input type="button" value="提交" onclick="go('/Ajax.w3c/servlet/test');"/> 
      </body>

    后端代码:

    public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            
            System.out.println("jinru fangfa ");
            String name = request.getParameter("123");
            System.out.println(name); 
            
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html");
            response.setHeader("Cache-Control","no-cache");
            PrintWriter out = response.getWriter();
            out.println("yes");
            out.flush();
            out.close(); 
        }
  • 相关阅读:
    云游四海
    保持良好的人际关系,赢得好人缘的八大诀窍
    二十三格经典的管理定律(建议收藏)
    游北湖公园有感
    如何成为领袖? 学习任正非小沃森郭士纳
    梦回江南
    观野花展有感
    爱一个人要爱多久
    醉卧山林
    游环岛路有感
  • 原文地址:https://www.cnblogs.com/zlay0701/p/5810406.html
Copyright © 2011-2022 走看看