前台调用
function checkLogin() { var name = $("#email").val(); var passward = $("#password").val(); $.ajax({ url: 'Ajax/web.ashx?method=login', type: 'POST', data: { "name": name, "passward": passward }, //type=1位撤销 dataType: 'json', success: function (dataInfo) { alert(dataInfo.IsSuccess); }, async: false }); }
后台 ajax
JsonConvert的使用需要添加引用Newtonsoft.Json.dll
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Runtime.Serialization.Json; using Putty.Common; using Newtonsoft.Json; namespace WebFrameDemo.web.Ajax { /// <summary> /// web 的摘要说明 /// </summary> public class web : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string strMethod = context.Request["method"]; if (!string.IsNullOrEmpty(strMethod)) { switch (strMethod) { case "login": GetLogin(context); break; } } } private void GetLogin(HttpContext context) { string _name = context.Request["name"]; string _password = context.Request["_password"]; CommonModel.resultMsg msg = new CommonModel.resultMsg(); msg.IsSuccess = false; msg.Info = "失败"; context.Response.Write(JsonConvert.SerializeObject(msg)); } public bool IsReusable { get { return false; } } } }
JsonConvert.SerializeObject也可以传List<T>等数据。