AJAX 在浏览器与 Web 服务器之间使用异步数据传输(HTTP 请求),这样就可使网页从服务器请求少量的信息,而不是整个页面。
AJAX 基于 Web 标准
AJAX 基于下列 Web 标准:
- JavaScript
- XML
- HTML
- CSS
在 AJAX 中使用的 Web 标准已被良好定义,并被所有的主流浏览器支持。AJAX 应用程序独立于浏览器和平台。
Deafault.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="XmlWeb._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript">
function ajaxFunction() {
var xmlhttp;
try {
xmlhttp = new XMLHttpRequest();
}
catch (e) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert("浏览器不支持ajax");
}
}
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
document.form1.time.value = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "time.aspx", true);
xmlhttp.send(null);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" name="username" οnkeyup="ajaxFunction();" />
<input type="text" id="time" name="time" />
</div>
</form>
</body>
</html>
time.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="time.aspx.cs" Inherits="XmlWeb.time" %>
<%
Response.Expires = -1;
Response.Write(DateTime.Now);
%>