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.
  • 相关阅读:
    解决跨域POST登录中IE不能正常工作的bug
    设置一个严格的SESSION过期时间
    一次不成功的脚本Hack[捕鱼达人游戏]
    页面高度定位
    简单实用的跨域表单POST提交
    最简单的记录程序运行时间的方法:[记录PHP程序运行消耗时间]
    火狐下的GreaseMonkey和Chrome下的tampermonkey使用手记
    console.log
    记录最近工作使用javascript对select[option]的操作
    ubuntu16.04让内核编译一次过的方法
  • 原文地址:https://www.cnblogs.com/ForDream/p/2137711.html
Copyright © 2011-2022 走看看