文章出處

小提醒,在jquery的ajax函數中,可以傳入3種類型的數據

1.文本:"uname=alice&mobileIpt=110&birthday=1983-05-12"

2.json對象:{uanme:'vic',mobileIpt:'110',birthday:'2013-11-11'}

3.json數組:

[
    {"name":"uname","value":"alice"},
    {"name":"mobileIpt","value":"110"},   
    {"name":"birthday","value":"2012-11-11"}
]
$.ajax()驗證登錄
<script type="text/javascript" language="javascript"> function IbtnEnter_onclick() { checklogin(); return false; } function checklogin() { if ($("#TxtUserName").val() == "") { alert("用戶名不能為空!"); $("#TxtUserName").focus(); return false; } if ($("#TxtPassword").val() == "") { alert("密碼不能為空!"); $("#TxtPassword").focus(); return false; } $.ajax({ type: "POST", url: "ajax/Handler.ashx?M=" + Math.random(), data: "username=" + $("#TxtUserName").val().toString() + "&pwd=" + $("#TxtPassword").val().toString(), success: function (data) { if (data == "1") { location.href = "index.aspx"; return true; } else { alert("請確認您輸入的用戶名或密碼輸入是否正確!"); $("#TxtUserName").val(""); $("#TxtPassword").val(""); $("#TxtUserName").focus(); return false; } } }) } </script> 一般處理程序 <%@ WebHandler Language="C#" Class="Handler" %> using System; using System.Web; using System.Data.SqlClient; using System.Web.SessionState;//繼承接口IReadOnlySessionState需要引入的命名空間 public class Handler : IHttpHandler, IRequiresSessionState { SqlHelper helper = new SqlHelper(); public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string username = context.Request.Params["username"].ToString().Trim(); string pwd = context.Request.Params["pwd"].ToString().Trim(); if (username != "" && pwd != "") { string sql = @"SELECT * FROM [USER] WHERE USERNAME='"+username+"' AND PASSWORD='"+pwd+"' "; if (!helper.Exists(sql)) { context.Response.Write("0"); } else { SqlDataReader reader = helper.ExecuteReader(sql); while (reader.Read()) { context.Response.Write("1"); context.Session["username"] = username.ToString().Trim(); context.Session["pwd"] = pwd.ToString().Trim(); } } } } public bool IsReusable { get { return false; } } }

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()