使用ashx作为服务;客户端通过ajax传输数据到ashx服务,直接上代码。

前端调用(使用jquery1.4.1版本,jquery1.9.1不支持这种写法):

    $.post("Handler/BasicService.ashx", { method: 'Login', 'username': escape($('#txtUserCode').val()), 'password': escape($('#txtPassword').val())) }, function (msg) {
                if (msg == 'success') {
                    window.location = 'index.aspx';
                }
                else {
                    alert(msg);
                }
            });

  

ashx服务:

 public void ProcessRequest(HttpContext context)
 {
            //不让浏览器缓存
            context.Response.Buffer = true;
            context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";
            context.Response.ContentType = "text/plain";
         
            Request = context.Request;
            Response = context.Response;
            Session = context.Session;
            Server = context.Server;
            string method = Request["Method"].ToString();//接收提交过来的Method参数
            MethodInfo methodInfo = this.GetType().GetMethod(method);//通过反射获取传递过来的Method(方法名称)类型
            methodInfo.Invoke(this, null);
 }

 

具体方法:

   public void Login()
   {
    UserModel user;
    string username = Request["username"].ToString(); //获取请求username参数值
    string password = Request["password"].ToString(); //获取请求password参数值
    //操作业务逻辑。。。
   }

 

相关文章:

  • 2022-03-09
  • 2021-10-06
  • 2022-01-25
  • 2021-08-28
  • 2021-12-28
  • 2022-12-23
  • 2021-12-15
  • 2021-06-26
猜你喜欢
  • 2021-06-25
  • 2021-08-20
  • 2021-07-27
  • 2021-11-18
  • 2021-11-07
相关资源
相似解决方案