ajax


//ajax 连接后端的代码


var
xhr = new XMLHttpRequest(); xhr.open("POST"/*传给后台的类型,常用get ,post*/, "TicketGetAllServlet"/*接收的servlet*/, true); xhr.responseType = "json";//响应类型 xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded"); xhr.send("pageSize=" +document.getElementById("pageSize").value+ "& pageNum=" +document.getElementById("pageNum").value);//传给后台的变量 xhr.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) {   var data = this.response;//接收后台返回的值
     //主要的功能
  }
}


或者

     var xhr = new XMLHttpRequest()
       var usernamestr = document.getElementById("username").value; var passwordstr = document.getElementById("password").value; xhr.open("post", "login?username="+usernamestr+"&password="+passwordstr/*变量的值也可以在这里写*/, true); xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xhr.send(); xhr.onreadystatechange = function(){ if (xhr.readyState == 4){ if (xhr.status == 200){ //主要的功能 } } }





原文地址:https://www.cnblogs.com/chenzezhan/p/7756442.html