在asp.net webForm开发中,用Jquery ajax调用aspx页面的方法常用的有两种:下面我来简单介绍一下。
[WebMethod]
public static string SayHello() { return "Hello Ajax!"; }前台jquery代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
$(function() { $("#btn").click(function() { $.ajax({ type: "post", //要用post方式 url: "Demo.aspx/SayHello",//方法所在页面和方法名 contentType: "application/json; charset=utf-8", dataType: "json", success: function(data) { alert(data.d);//返回的数据用data.d获取内容 }, error: function(err) { alert(err); } }); }); }); |
html代码:
|
1
2
3
4
5
|
<form id="form1" runat="server"> <div> <asp:Button ID="btn" runat="server" Text="验证用户" /> </div> </form> |
type: "POST",
url: "S_CBFBM.ashx", data: { ZBM: p_zdm }, beforeSend: function() { //$("#div_load").visible = "true; }, success: function(msg) { //$("#div_load").visible = false; $("#ds").html("<p>" + msg + "</p>"); $("#CBFBM").val(msg); } });ashx.cs代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<%@ WebHandler Language="C#" Class="AjaxHandler" %> using System; using System.Web; public class AjaxHandler : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; if (context.Request["name"].ToString() == "admin" && context.Request["pass"].ToString() == "admin") { context.Response.Write("Y"); } else { context.Response.Write("N"); } } public bool IsReusable { get { return false; } } } |
以上所述是小编给大家介绍的jQuery ajax调用后台aspx后台文件的两种常见方法(不是ashx),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!