Ajax 调用后台方法
2012-08-27 15:28
232 查看
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace FeiMiao.UI.Ajax { /// <summary> /// AjaxPostHandlers 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Post : IHttpHandler { HttpContext httpContext; public void ProcessRequest(HttpContext context) { httpContext = context; context.Response.ContentType = "text/plain"; switch (getFormString("method")) { case "HelloWorld": HelloWorld(); break; case "Hello": Hello(); break; } } private void Hello() { string input = getFormString("input"); httpContext.Response.Write("你传入的参数:" + input); } private void HelloWorld() { httpContext.Response.Write("HelloWorldTest"); } public bool IsReusable { get { return false; } } public string getFormString(string key) { string s = HttpContext.Current.Request.Form[key]; return s == null ? "" : s; } } }
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="FeiMiao.UI.Default" %> <!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 runat="server"> <title></title> <script src="Scripts/jquery-1.8.0.min.js" type="text/javascript"></script> <script type="text/javascript" language="javascript"> function Hello() { $.post("Ajax/Post.ashx", { method: "HelloWorld" }, function (data) { if (null != data && data != "") { alert(data); } }, "text"); } function Test() { $.post("Ajax/Post.ashx", { method: "Hello", input: "heihei" }, function (data) { if (null != data && data != "") { alert(data); } }, "text"); } </script> </head> <body> <form id="form1" runat="server"> <div> <input type="button" value="不带参数" onclick="Hello()" /> <input type="button" value="带参数" onclick="Test()" /> <asp:Button ID="btnTest" runat="server" OnClick="btnTest_Click" Text="Button" /> <br /> </div> </form> </body> </html>
相关文章推荐
- 基于JavaScript如何实现ajax调用后台定义的方法
- ajax 同步调用 返回字符串 java例子(全)及 从后台返回中文乱码解决方法
- JQuery .ajax方法直接调用aspx后台代码
- ASP.Net Ajax应用初步:使用AJAX直接调用后台方法
- AjaxPro.dll,asp.net 前台js调用后台方法(无刷新)
- 客户端通过Ajax调用后台方法返回DataSet,Ilist,ilist,T,对象
- Asp.net中JQuery、ajax调用后台方法总结
- js调用后台方法 (AjaxPro.2.dll的基本使用)
- 利用JQuery的$.ajax()可以很方便的调用asp.net的后台方法
- ajax 调用后台方法大家可以讨论下
- Asp.net AJAX调用后台静态方法总结
- .NET 中ajax如何调用后台.cs文件中的方法
- 利用sender的Parent获取GridView中的当前行 不用AJAX实现前台JS调用后台C#方法(小技巧) AjaxControlToolkit的CalendarExtender的本地化
- $.ajax $.function 调用后台方法
- ajax调用后台方法
- 不用AJAX实现前台JS调用后台C#方法
- 注册JQuery使用ajax调用后台方法,判断用户名、邮箱是否存在,并显示隐藏提示
- ExtJs后台返回success为false,Ext.ajax.request也用调用success方法,解决方法
- ajax 前台js调用后台方法 (转)
- JS Ajax --- 使用AjaxPro 调用后台方法 操作数据库