<!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>
    <title>Ajax</title>
    <script src="../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            //$.get / $.post内部均调用$.ajax

            //无参数ajax请求
            $("#btnGetTime").click(function () {
                $.post("Ajax14-1.ashx", function (Returns, Status) {
                    if (Status == "success")
                        $("#txtTime").val(Returns);
                    else
                        alert("请求失败");
                });
            });

            //有参数
            $("#btnInput").click(function () {
                $.post("Ajax14-2.ashx", { "input": $("#txtInput").val() }, function (Returns, Status) {
                    if (Status == "success")
                        alert(Returns);
                    else
                        alert("请求失败");
                });
            });
        });
    </script>
</head>
<body>
    <input id="txtTime" type="text" readonly="readonly" />
    <input id="btnGetTime" type="button" value="获得时间" />

    <input id="txtInput" type="text" />
    <input id="btnInput" type="button" value="发送字符串" />
</body>
</html>

  Ajax14-1.ashx Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ImitationBaiduPostBar.JQuery
{
	/// <summary>
	/// Ajax14 的摘要说明
	/// </summary>
	public class Ajax14 : IHttpHandler
	{

		public void ProcessRequest(HttpContext context)
		{
			context.Response.ContentType = "text/plain";
			//context.Response.Write("Hello World");
			context.Response.Write(DateTime.Now.ToString());
		}

		public bool IsReusable
		{
			get
			{
				return false;
			}
		}
	}
}

  Ajax14-2.ashx Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ImitationBaiduPostBar.JQuery
{
	/// <summary>
	/// Ajax14_2 的摘要说明
	/// </summary>
	public class Ajax14_2 : IHttpHandler
	{

		public void ProcessRequest(HttpContext context)
		{
			context.Response.ContentType = "text/plain";
			//context.Response.Write("Hello World");
			context.Response.Write(string.Format("你输入了:{0}",
				context.Request.Form["input"]));
		}

		public bool IsReusable
		{
			get
			{
				return false;
			}
		}
	}
}