//客户端使用方法
<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Response.Write("aaaa");
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="JS/jquery-1.4.4.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
function btnClick() {
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //创建对象
if (!xmlhttp) {
alert(\'创建xmlhttp对象异常\');
return false;
}
xmlhttp.open("post", "datatime.ashx?ts" + Date(), false); //准备向服务器datatime.ashx发送post请求
//XMLHTTP默认(也推荐)不是同步请求的,也就是OPEN方法并不像WebClient的DownloadString
//那样把服务器返回的数据那的哦啊才返回,是异步的,因此需要监听onreadystatechange事件
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) //代表是正确的
{
if (xmlhttp.status == 200) //代表是正确的,300代表重定向,400代表权限的问题,500代表错误
{
document.getElementById("text").value = xmlhttp.responseText; //responseText属性为服务器返回的文本
}
else {
alert(\'服务器返回错误!!!\');
}
}
}
xmlhttp.send(); //这是才开始发送请求
}
//Ajax版本
function AjaxbtnClick() {
$("text").val();
$.post("datatime.ashx", {"ts":4}, function (data, textStatus) { alert(data); alert(textStatus); });
//data返回的信息,textStatus返回状态
}
</script>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<div>
<input type="text" id="text" /><input type="button" value="确定" onclick="btnClick()" />
<input type="button" value="Ajax版" onclick="AjaxbtnClick()" />
</div>
</form>
</body>
</html>
//服务器端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Ajax使用
{
/// <summary>
/// datatime 的摘要说明
/// </summary>
public class datatime : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write(DateTime.Now.Date.ToShortDateString() + " " + context.Request.Files.Count); //返回文本
}
public bool IsReusable
{
get
{
return false;
}
}
}
}