前台頁面AjaxTest.htm,內容如下
代碼如下 |
複製代碼 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" http://www.111cn.net> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>本頁用不同的方式與後台進行互動</title> <script type="text/javascript" src="JS/jquery-1.4.2.min.js"></script> <script type="text/javascript" > //使用jquery庫進行ajax互動 $(document).ready(function(){ //進行一個ajax請求,command告訴後台調用哪個方法 $.get("Handler.ashx",{command:"method1",value:"chentao"},function(data){ alert(data); }); //進行一個ajax請求,command告訴後台調用method2方法 $.get("Handler.ashx",{command:"method2",value:"tangyu"},function(data){
alert(data); }) </script> </head> <body> </body> </html> |
後台建立一個Handler.ashx頁面 內容如下
代碼如下 |
複製代碼 |
<%@ WebHandler Language="C#" class="Handler" %> using System; using System.Web; public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; if (context.Request["command"]!=null) { //得到前台傳過來的command,確定調用哪個方法 string command = context.Request["command"].ToString(); string data = context.Request["value"].ToString(); switch (command) { case "method1": method1(context); break; case "method2": method2(context); break; default: break; } } } public bool IsReusable { get { return false; } } public void method1(HttpContext context) { context.Response.Write("hello,"+context.Request["value"].ToString()); } public void method2(HttpContext context) { context.Response.Write("hello,"+context.Request["value"].ToString()); }
} |
如果有多個方法,switch case裡的判斷將會很多.考慮用更簡單的方法.使用反射
代碼如下 |
複製代碼 |
<%@ WebHandler Language="C#" class="Handler" %> using System; using System.Web; public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; if (context.Request["command"] != null) { // string command = context.Request["command"].ToString(); System.Reflection.MethodInfo method = this.GetType().GetMethod(command); if (method != null) { method.Invoke(this, new object[] { context}); } } } public bool IsReusable { get { return false; } } public void method1(HttpContext context) { context.Response.Write("hello"+context.Request["value"].ToString()); } public void method2(HttpContext context) { context.Response.Write("hello,"+context.Request["value"].ToString()); } } |
使用反射大大簡化了程式.
=====================================================
使用aspx頁面與ajax互動
建立一個aspx頁面 WebMethod.aspx
將WebMethod.aspx頁裡的多餘部分刪除,只保留
代碼如下 |
複製代碼 |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebMethod.aspx.cs" Inherits="WebMethod" %> |
這一條語句
WebMethod.aspx.cs內容如下
代碼如下 |
複製代碼 |
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.Services; using System.Reflection; public partial class WebMethod : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string methodName = HttpContext.Current.Request.PathInfo.Substring(1); // Response.Write(methodName); MethodInfo method = this.GetType().GetMethod(methodName); if (method != null) { Response.Write(method.Invoke(this,new object[]{})); } // Response.Write(GetResult()); } [WebMethod(EnableSession=true)] public string GetResult() { //return "hello"; if (HttpContext.Current.Request["name"] != null) { string value = HttpContext.Current.Request["name"].ToString(); //HttpContext.Current.Request.PathInfo; return "{'name':'"+value+"'}"; } else { return "{name:'error'}"; } } } test.html頁面與WebMethod.aspx頁面進行ajax互動 test.html頁面內容 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>使用aspx頁面進行互動</title> <script type="text/javascript" src="JS/jquery-1.4.2.min.js"></script> <script type="text/javascript" > $(document).ready(function(){ $.ajax({ type: "POST", url: "WebMethod.aspx/GetResult", data: "name=chentao", dataType: "text", success: function(d){ alert(d); } }); }); </script> </head> <body> </body> </html> |