jquery(ajax)+ashx简单开发框架(原创)
使用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参数值 //操作业务逻辑。。。 }