一个简单的AJAX实现(HELLO AJAX)
来源:程序员人生 发布时间:2013-12-16 22:14:02 阅读次数:2757次
客户端部分:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html"/>
<script language="javascript">
var ajax;
function createAjax()
{
if(window.ActiveXObject)
{
try
{
return new ActiveXObject("Msxm12.XMLHTTP");
}
catch(e)
{
try
{
return new
ActiveXObject("Microsoft.XMLHTTP");
}
catch(e2)
{
return null;
}
}
}
else if(window.XMLHttpRequest)
{
return new XMLHttpRequest();
}
else
{
return null;
}
}
function onRcvData()
{
if(ajax.readyState==4)
{
if(ajax.status==200)
{
var content=document.getElementById('content');
content.innerHTML=ajax.responseText;
}
else
{
alert("error");
}
}
}
function ajaxSendRequest(uri)
{
ajax=createAjax();
if(!ajax)
{
alert("no");
return 0;
}
ajax.onreadystatechange=onRcvData;
ajax.open("GET",uri,true);
ajax.send("");
}
</script>
<title>Hello AJAX - Liehuo.Net</title>
</head>
<body>
<div id="content"></div>
<br>
<input type="button" value="Hello"
onclick="ajaxSendRequest('http://localhost:8080/test/hello.jsp')">
</body>
</html>
服务器端部分(hello.jsp)
<html>
<head>
<title>hellp - Liehuo.Net</title>
</head>
<body>
<%
out.println("HELLO AJAX");
%>
</body>
</html>
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠