zoukankan      html  css  js  c++  java
  • ajax交互

    1.servlet get 

    servlet:

    response.setContentType("text/html;charset=utf-8");
    PrintWriter out=response.getWriter();
    out.write("hello ajax");
    System.out.println("ServletDemo1执行了");

    java:

    <script type="text/javascript">
    window.onload=function(){
    document.getElementById("b1").onclick=function(){
    var xhr=createXmlHttpRequest();
    xhr.onreadystatechange=function(){
    if(xhr.readyState==4){
    if(xhr.status==200||xhr.status==304){
    var data =xhr.responseText;
    alert(data);
    document.getElementById("d1").innerHTML=data;
    }
    }
    }
    xhr.open("GET","/ajaxdayday28/servlet/ServletDemo1?time="+new Date().getTime());
    xhr.send(null);
    };
    }
    function createXmlHttpRequest(){
    var xmlHttp;
    try{
    xmlHttp=new XMLHttpRequest();
    }catch(e){
    try{
    xmlHttp=new ActiveXobject("Msxml2.XMLHTTP");
    }catch(e){
    try{
    xmlHttp=new ActiveXobject("Microsoft.XMLHTTP");
    }catch(e){}
    }
    }
    return xmlHttp;
    }
    </script>

    2.servlet post

    servlet:

    String username= request.getParameter("username");
    String pwd= request.getParameter("pwd");

    java:

    <script type="text/javascript">
    window.onload=function(){
    document.getElementById("b1").onclick=function(){
    var xhr=createXmlHttpRequest();
    xhr.onreadystatechange=function(){
    if(xhr.readyState==4){
    if(xhr.status==200||xhr.status==304){

    }
    }
    }
    xhr.open("POST","/ajaxdayday28/servlet/ServletDemo2?time="+new Date().getTime());
    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xhr.send("username=admin&pwd=123");
    };
    }
    function createXmlHttpRequest(){
    var xmlHttp;
    try{
    xmlHttp=new XMLHttpRequest();
    }catch(e){
    try{
    xmlHttp=new ActiveXobject("Msxml2.XMLHTTP");
    }catch(e){
    try{
    xmlHttp=new ActiveXobject("Microsoft.XMLHTTP");
    }catch(e){}
    }
    }
    return xmlHttp;
    }
    </script>

  • 相关阅读:
    2011/6/24 数据库分析
    项目代码总结
    背景透明 by sofish
    ie6 reflow bug
    ID与CLASS的使用技巧
    CSS浮动属性Float详解 by 帕兰
    javascript闭包 by 李松峰
    详解CSS选择器、优先级与匹配原理
    垂直对齐:verticalalign属性 by ddcatlee
    行高lineheight,以及基线、顶线、中线和底线,还有内容区域、行内框和行框 by 豆豆猫的窝
  • 原文地址:https://www.cnblogs.com/zszitman/p/4500481.html
Copyright © 2011-2022 走看看