zoukankan      html  css  js  c++  java
  • JQuery[13] Ajax的简单应用

    <!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;
    			}
    		}
    	}
    }
    

      

    My New Blog : http://blog.fdlife.info/ The more you know, the less you believe.
  • 相关阅读:
    ural 1110,快速幂
    ural 1109,NYOJ 239,匈牙利算法邻接表
    CodeBlocks养眼的colour theme
    UVa 10047,独轮车
    UVa 10054,欧拉回路
    UVa 11624,两次BFS
    hiho一下,第115周,FF,EK,DINIC
    Poj(1220),hash
    2013 Asia Regional Changchun I 题,HDU(4821),Hash
    UVa 213,World Finals 1991,信息解码
  • 原文地址:https://www.cnblogs.com/ForDream/p/2137711.html
Copyright © 2011-2022 走看看