Asp.net Jquery Ajax 实例

来源:互联网 发布:mac代理设置 编辑:程序博客网 时间:2024/05/20 04:28

最新在做一个项目用到ajax交互,捣鼓了一会写了一个小实例,贴出来供将要学习Jquery与Ajax朋友分享。

 

jquery 库最新版下载jquery-1.4.2min.js

jquery 1.3.2mini版:jquery-1.3.2min.js

中文手册下载地址(超酷版):jquery12api

 

前台代码:

  

[c-sharp:firstline[1]] view plaincopy
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ajax.aspx.cs" Inherits="ajax" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3.   
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title>Jquery Ajax实例</title>  
  7.     <mce:script type="text/javascript" src="script/jquery-142min.js" mce_src="script/jquery-142min.js"></mce:script>  
  8.     <mce:script type="text/javascript"><!--  
  9. $(function() {  
  10.     $("#dbtn").click(function(){  
  11.         $.ajax({  
  12.                type: "POST",  
  13.                //dataType:"Text",  
  14.                url:"Handler.ashx",  
  15.                data:{name:"admin",pass:"admin"},  
  16.                beforeSend:function(){  
  17.                $("#ds").html("loading");  
  18.                },  
  19.                success: function(msg){  
  20.                 $("#ds").html("<p>"+msg+"</p>");  
  21.                }  
  22.         });       
  23.           
  24.     });  
  25. });  
  26. // --></mce:script>  
  27. </head>  
  28. <body>  
  29.     <form id="form1" runat="server">  
  30.     <div>  
  31.     <div id="ds"><p>我是AJAX原来的文字!</p></div>  
  32.     <input type="button" value="提交AJAX测试" id="dbtn" name="dbtn" />  
  33.     </div>  
  34.     </form>  
  35. </body>  
  36. </html>  
  

 

Handler.ashx:

[c-sharp:firstline[1]] view plaincopy
  1. <%@ WebHandler Language="C#" Class="Handler" %>  
  2.   
  3. using System;  
  4. using System.Web;  
  5. using System.Data.SqlClient;  
  6.   
  7. public class Handler : IHttpHandler {  
  8.       
  9.     public void ProcessRequest (HttpContext context) {  
  10.         context.Response.ContentType = "text/plain";  
  11.           
  12.         //context.Response.Write("Hello World");  
  13.         if (context.Request["name"].ToString() == "admin" && context.Request["pass"].ToString() == "admin")  
  14.         {  
  15.             context.Response.Write("Y");  
  16.         }  
  17.         else  
  18.         {  
  19.             context.Response.Write("N");  
  20.         }  
  21.     }  
  22.     public bool IsReusable  
  23.     {  
  24.         get  
  25.         {  
  26.             return false;  
  27.         }  
  28.     }  
  29.       
  30. }