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(); 
        }
  • 相关阅读:
    [LOJ 6436][PKUSC2018] 神仙的游戏
    [BZOJ 2653] middle
    [WC2018] 州区划分
    [BZOJ 4556][Tjoi2016&Heoi2016]字符串
    [BZOJ 3514]Codechef MARCH14 GERALD07加强版 (CHEF AND GRAPH QUERIES)
    [BZOJ 4573][ZJOI 2016]大♂森林
    Problem 2322. -- [BeiJing2011]梦想封印
    [BZOJ 2555] SubString
    [日常] NOIWC2019 冬眠记
    [BZOJ 4036][HAOI2015]按位或
  • 原文地址:https://www.cnblogs.com/zlay0701/p/5810406.html
Copyright © 2011-2022 走看看