AJAX实现无刷新登录功能的方法
YPE html>
æè¿å°ç¼å¦ä¼äºAJAXå®ç°æ å·æ°ç»å½åè½çæ¹æ³ï¼é£ä¹è¶çä»å¤©ææ¶é´å°±ä¸ºå¤§å®¶è¯¦ç»ä»ç»ä¸ä¸å§ï¼å¤§ä½çææå¦ä¸ï¼å¤§å®¶å¯ä»¥åèåèã
ç¹å»ç»å½æé®æ¶å¼¹åºç»å½çªå£,è¾å ¥æ£ç¡®çç¨æ·åå¯ç åç¹å»ç»å½åç»å½çªå£å ³é,ç¶ææ¹ä¸ºå½åç¨æ·å.
第ä¸æ¥:
é¦å å¼¹åºçªå£ä½¿ç¨çæ¯jquery-uiä¸çæ§ä»¶,第ä¸æ¥è¦å¦ä¼å¦ä½ä½¿ç¨.
æå¼è§£ååçjquery-UIä¸çdevelopment-bundle->demos
,æ¾å°index.html,éæ©dialogä¸çmodel dialog,å³é®æ¥çæºç ,è§å¯å¦ä½ä½¿ç¨è¯¥æ§ä»¶,æ¾å°ä¸å¥å
³é®ä»£ç :$("#dialog-modal").dialog({height: 140,modal: true});
è¿æ¯ç¨äºæ¾ç¤ºç,æå¼model messageä¸çæºç ,æ¾å°å
³éçå
³é®ä»£ç :$(this).dialog('close');æäºè¿ä¸¤å¥ä»£ç ,å¯ä»¥æ§å¶çªå£çæ¾ç¤ºä¸å
³é,å¯ä»¥è¿è¡ä¸ä¸æ¥äº.使ç¨æ¶éå¤å¶jquery-uiå¼åå
çcssæ件夹,jsæ件夹å°é¡¹ç®ä¸.
第äºæ¥:
å¨è¿éå è´´åºå¤çAJAX请æ±çä¸è¬å¤çç¨åºç代ç ,è½ç¶æ£çåçæ¶åé½æ¯ç¨å°åå,ä½è¿éä¸å¯è½ä¸æ¥ä¸æ¥è¯¦ç»ååº,为äºä¾¿äºç解,å å°ä¸è¬å¤çç¨åºä»£ç è´´åºæ¥:
1.IsLogin.ashx,ç¨äºå¤æç¨æ·æ¯å¦ç»å½,ç»å½åè¿åç¨æ·å.è¿é注æ,å¨ä¸è¬å¤çç¨åºä¸è¦ä½¿ç¨session,å¿ é¡»å¼å ¥using System.Web.SessionStateä¸è¦å®ç°IRequiresSessionStateæ¥å£
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.SessionState; namespace AJAXæ å·æ°ç»å½.AJAX { ////// IsLogin çæè¦è¯´æ /// public class IsLogin : IHttpHandler,IRequiresSessionState { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; if (context.Session["userName"] != null) { string userName = context.Session["userName"].ToString(); context.Response.Write("yes|"+userName); } else { context.Response.Write("no"); } } public bool IsReusable { get { return false; } } } }
2.CheckLogin.ashx,ç¨äºæ£æµç¨æ·è¾å ¥ç¨æ·åå¯ç æ¯å¦å¹é ,æ£ç¡®åè¿åyes,é误è¿åno,è¿é为äºç®ä¾¿æ²¡æè¿æ¥æ°æ®åº.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.SessionState; namespace AJAXæ å·æ°ç»å½.AJAX { ////// CheckLogin çæè¦è¯´æ /// public class CheckLogin : IHttpHandler,IRequiresSessionState { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string userName = context.Request["userName"]; string password=context.Request["password"]; if (userName=="admin"&&password=="admin") { context.Session["userName"] = "admin"; context.Response.Write("ok"); } else { context.Response.Write("no"); } } public bool IsReusable { get { return false; } } } }
3.LoginOut.ashx,ç¨äºæ§å¶ç¨æ·ç»åº,设置session为空.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.SessionState; namespace AJAXæ å·æ°ç»å½.AJAX { ////// LoginOut çæè¦è¯´æ /// public class LoginOut : IHttpHandler,IRequiresSessionState { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; context.Session["userName"] = null; } public bool IsReusable { get { return false; } } } }
ä¸è¬å¤çç¨åºå°±ç»æäº,ä¸é¢è´´åºä¸»çé¢ç代ç :
以ä¸æè¿°æ¯å°ç¼ç»å¤§å®¶AJAXå®ç°æ å·æ°ç»å½åè½çæ¹æ³ï¼éå°ç±»ä¼¼é®é¢çæåä¸å¦¨åé´å¦ä¹ ä¸ï¼å¦æè¿æçé®ï¼è¯·å¨æ¬æåºé¨çè¨å§ã