簡單的請求-處理-響應
登錄案例:
新建網站,在網站中添加一個longth.html(靜態網頁)文件和Handler.ashx(一般處理程序)文件
1.longth.html文件
HTML代碼
<form action="Handler.ashx" method="post"> 註:提交到Handler.ashx(一般處理程序) 請求方式post
賬號:<input id="name" name="name" type="text" /><br />
密碼:<input id="Password" name="password" type="password" /><br />
<input id="Button1" type="submit" value="登錄" /> 註:提交按鈕
</form>
jquery代碼
<script src="jq/jquery-3.2.1.min.js"></script>
$(function () {
$("form").submit(function () { 註:表單提交事件(submit())
var name = $("#name").val().trim();
var password = $("#Password").val().trim();
if(name=="")
{
alert("賬號不能為空");
$("#name").focus();
return false; 註:如果賬號為空就返回false不提交
}
else if(password=="")
{
alert("密碼不能為空");
$("#Password").focus();
return false; 註:如果密碼為空就返回false不提交
}
return true; 註:如果都不為空就返回true提交表單
});
});
2.Handler.ashx(一般處理程序)
string account = context.Request["account"]; 註:獲得name屬性為account的文本框裏的值
string password = context.Request["password"]; 註:獲得name屬性為password的文本框裏的值
if (account == "" && password == "")
{
context.Response.Write("賬號或密碼為空"); 註;如果account或password等於空就彈出賬號或密碼為空
}
string sql = "select COUNT(*) from name where [email protected]
SqlParameter[] sp = {
new SqlParameter("@name",account),
new SqlParameter("@password",password)
};
int i = Convert.ToInt32(SQLHelper.ExecuteScalar(sql, sp)); 註:調用SQLHelper類裏的ExecuteScalar方法然後轉換為int類型
if (i == 1)
{
context.Response.Write("登錄成功"); 註:如果查詢的數距有就返回1,彈出登錄成功
}
else
{
context.Response.Write("登錄失敗"); 註:如果查詢的數距沒有,彈出登錄失敗
}
簡單的請求-處理-響應