Asp.net Jquery Ajax 实例

本文通过一个具体的示例展示了如何使用JQuery发起Ajax请求,并在后台处理这些请求。该示例涉及了按钮点击事件的处理、加载提示的显示以及根据响应更新页面内容等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

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

jquery 1.3.2mini版:jquery-1.3.2min.js

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

 

前台代码:

  

[c-sharp]  view plain copy
  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]  view plain copy
  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. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值