zoukankan      html  css  js  c++  java
  • jQuery ajax调用后台aspx后台文件的两种常见方法(不是ashx)

    在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),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

  • 相关阅读:
    Linux下vi命令大全
    Ubuntu的cron日志在哪里?
    如何使用DNN中的Calendar控件
    对DNN的理解:
    “SQL Server does not allow remote connections”错误的解决
    如何去除Search Skin ojbect中的"web"和"site"选项按键
    DNN发邮件通知4.8.2有漏洞,最好升级到新版本
    模块开发中一点疑惑?
    经典ASP代码大集合
    漂亮button
  • 原文地址:https://www.cnblogs.com/gycnet/p/5977530.html
Copyright © 2011-2022 走看看