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>

  • 相关阅读:
    CoreOS 手动更新
    阿里云ECS安装 CoreOS
    CoreOS 手动升级篇
    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
    Microsoft.NETCore.App 版本不一致导致的运行失败
    设计模式之观察者模式
    设计模式之策略模式
    树的广度优先遍历和深度优先遍历(递归非递归、Java实现)
    设计模式之组合模式
    设计模式之外观模式
  • 原文地址:https://www.cnblogs.com/zszitman/p/4500481.html
Copyright © 2011-2022 走看看