標籤:style blog http color java os io 檔案
前台代碼:
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <title>JS直接調用C#方法</title> <script type="text/javascript" src="/Scripts/Jquery-1.9.1.js"></script> <script type="text/javascript"> $(function () { $("#btnCall").click(function () { $.post("/apps/demo1/filters/OperateHandler.ashx?test=12399", { Id: 1, Name: "test" }, function (data) { }, "text"); }); }); </script></head><body> <input type="button" id="btnCall" value="Call" /></body></html>
?後面的參數可以用context.Request.QueryString來訪問,json對象裡的參數【{Id:1,Name:"test"}】可以用context.Request.Form來訪問
建立一個ashx檔案,ashx檔案代碼如下【內容為預設建立內容,未作修改】:
public class OperateHandlerashx : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Write("Hello World"); } public bool IsReusable { get { return false; } } }