Java Web基础回顾 —AJAX

  1. Ajax( AsynchronousJavaScript and XML ): 异步的javascript和XML。

  2. Ajax中一个重要的对象是:XMLHttpRequest。

  3. Ajax准备向服务器端发送请求:
    xmlHttpRequest.open("GET","AjaxServlet",true);
    设置回调函数:

     xmlHttpRequest.onreadystatechange = ajaxcallback; 
    

    发送请求:

     xmlHttpRequest.send(null);
    
  4. 回调函数:

     function ajaxcallback(){
       if(xmlHttpRequest.readyState == 4){
            if(xmlHttpRequest.status == 200){
                 var responseTxt = xmlHttpRequest.responseText;
                 document.getElementById("showname").innerHTML = responseTxt;
            }
       }
     }	
    
  5. 使用POST方式请求时需要加:

     xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
原文地址:https://www.cnblogs.com/nextStep/p/6694865.html