码迷,mamicode.com
首页 > Web开发 > 详细

ajax交互

时间:2015-05-13 16:05:13      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

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>

ajax交互

标签:

原文地址:http://www.cnblogs.com/zszitman/p/4500481.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!