先来看一下javascript的。
var httpxml;
httpxml = new XMLHttpRequest();
httpxml.onreadystatechange=function(){ //当服务器响应就绪时执行函数(就是说服务器准备好了你可以发请求了)
if(httpxml.status == 200 && httpxml.readyState == 4){
//这里200和4代表响应的状态,当服务器准备好了就返回这两个数值
document.getElementById("ss").innerHTML = httpxml.responseText //改变id为ss的标签值
//httpxml.responseText 代表服务器返回的数据
}
}
httpxml.open("post", "com/Servlet",true); //open发送请求的方式(get或post),请求的服务器代码(页面),是否异步(true)
httpxml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");//表单请求要加这个
httpxml.send(); //发送(表单的话就在send中填写发送的数据信息)
jquery的
$.ajax({
type:"post", //请求方式
url:"com/Servlet", // 请求服务器端的页面
data:null, // 你要发送给服务器的数据
success:function(data,state){ //服务器返回给你的数据data
alert(data);
}
})