标签:
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>
标签:
原文地址:http://www.cnblogs.com/zszitman/p/4500481.html