001 |
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> |
002 |
<%request.setCharacterEncoding("UTF-8");%> |
003 |
<html> |
004 |
<head> |
005 |
<title>测试Ajax页面</title> |
006 |
</head> |
007 |
<body> |
008 |
<table width="100%" height="100%" > |
009 |
<td align="center" valign="center" > |
010 |
<form action="" method="post" onsubmit=""> |
011 |
<table border="3" > |
012 |
<tr> |
013 |
<td> |
014 |
<input name="试验返回String" type="button" value="试验返回String" onclick="testAjaxForString();" style="width:140px;"> |
015 |
</td> |
016 |
</tr> |
017 |
<tr> |
018 |
<td> |
019 |
<input name="试验返回javascriptString" type="button" value="试验返回javascriptString" onclick="testAjaxForObject();" style="width:140px;"> |
020 |
</td> |
021 |
</tr> |
022 |
<tr> |
023 |
<td> |
024 |
<input name="试验返回json" type="button" value="试验返回json" onclick="testAjaxForJson();" style="width:140px;"> |
025 |
</td> |
026 |
</tr> |
027 |
<tr> |
028 |
<td> |
029 |
<input name="试验返回xml" type="button" value="试验返回xml" onclick="testAjaxForXml();" style="width:140px;"> |
030 |
</td> |
031 |
</tr> |
032 |
</table> |
033 |
</form> |
034 |
</td> |
035 |
</table> |
036 |
</body> |
037 |
</html> |
038 |
|
039 |
<script type="text/javascript"> |
040 |
var xmlHttp ; |
041 |
function createXmlHttpRequest(){ |
042 |
if(window.ActiveXObject){ |
043 |
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") ; |
044 |
}else if(window.XMLHttpRequest){ |
045 |
xmlHttp = new XMLHttpRequest(); |
046 |
} |
047 |
} |
048 |
/*返回String*/ |
049 |
function testAjaxForString(){ |
050 |
createXmlHttpRequest(); |
051 |
var url = "HelloWorld?"; |
052 |
var queryString = "name=songwei"; |
053 |
url += queryString ; |
054 |
// xmlHttp.onreadystatechange = parseHello ;//如果是异步,既open中最后一个参数为true.则打开该选项. |
055 |
xmlHttp.open("post",url,false); |
056 |
xmlHttp.send(null); |
057 |
alert(xmlHttp.responseText); //如果是字符串或者xml. |
058 |
} |
059 |
|
060 |
/*返回JsObject*/ |
061 |
function testAjaxForObject(){ |
062 |
createXmlHttpRequest(); |
063 |
var url = "HelloWorldForObject?"; |
064 |
var queryString = "name=songwei"; |
065 |
url += queryString ; |
066 |
// xmlHttp.onreadystatechange = parseHello ; |
067 |
xmlHttp.open("post",false); |
068 |
xmlHttp.send(null); |
069 |
alert(xmlHttp.responseText); |
070 |
var o = eval(xmlHttp.responseText); |
071 |
alert("username="+o.username); |
072 |
alert("password="+o.password); |
073 |
var farray = o.friend ; |
074 |
var fname =""; |
075 |
for(var i=0;i<farray.length;i++){ |
076 |
fname += farray[i]+" "; |
077 |
} |
078 |
alert("friend list ="+fname); |
079 |
} |
080 |
//返回Json |
081 |
function testAjaxForJson(){ |
082 |
createXmlHttpRequest(); |
083 |
var url = "HelloWorldForJson?"; |
084 |
var queryString = "name=songwei"; |
085 |
url += queryString ; |
086 |
xmlHttp.open("post",false); |
087 |
xmlHttp.send(null); |
088 |
alert(xmlHttp.responseText); |
089 |
var o = eval("(" + xmlHttp.responseText + ")"); |
090 |
alert(o.userInfo.username); |
091 |
alert(o.userInfo.password); |
092 |
alert(o.userInfo.friends[0]); |
093 |
alert(o.userInfo.friends[1]); |
094 |
} |
095 |
|
096 |
//返回XML |
097 |
function testAjaxForXml(){ |
098 |
createXmlHttpRequest(); |
099 |
var url = "HelloWorldForXML?"; |
100 |
var queryString = "name=songwei"; |
101 |
url += queryString ; |
102 |
xmlHttp.open("post",false); |
103 |
xmlHttp.send(null); |
104 |
alert(xmlHttp.responseXML); |
105 |
alert ("tagName: " + xmlHttp.responseXML.documentElement.tagName); |
106 |
var xmlDoc = xmlHttp.responseXML ; |
107 |
var xmlRoot=xmlDoc.documentElement; |
108 |
|
109 |
var friendsItem=xmlRoot.getElementsByTagName("friends"); |
110 |
alert(friendsItem[0].firstChild.firstChild.data); |
111 |
alert(friendsItem[0].firstChild.nextSibling.firstChild.data); |
112 |
var usernameItem = xmlRoot.getElementsByTagName("username"); |
113 |
alert(usernameItem[0].firstChild.data); |
114 |
|
115 |
} |
116 |
</script> |