-------------------------------------------jQury+Ajax调用后台方法-------------------------------------------
JS:
function CarState(carID) {
$.ajax({
type: "Post",
url: "ShowMap.aspx/GetCarState",
//方法传参的写法一定要对,str为形参的名字
data: "{'carid':'" + carID + "' }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
//返回的数据用data.d获取内容
alert(data.d);
}
});
}
后台:
using System.Web.Script.Services;
using System.Web.Services;
[WebMethod]
public static string GetCarState(string carid)
{
return "Hello world!";
}
-------------------------------------------jQuery+Ajax提交表单-------------------------------------------
HTML:
<form id="form1" class="form form-horizontal" action="Login.aspx" method="post" style="margin-left:-10px;margin-top:-10px">
<div class="row cl" style="margin-top:60px;">
<div class="formControls col-8" style="margin-top:-5px;">
<input id="t_name" name="t_name" type="text" placeholder="账户" autocomplete="off" class="input-text size-L" value="" />
</div>
</div>
<div class="row cl">
<div class="formControls col-8" style="margin-top:-5px;">
<input id="t_pwd" name="t_pwd" type="password" placeholder="密码" autocomplete="off" class="input-text size-L" value="" />
</div>
</div>
<div class="row">
<div class="formControls col-8 col-offset-3">
<input id="b_submit" type="submit" class="btn btn-success radius size-L" value="登录" onclick="validate(); return false;" />
<span id="loading" class="btn btn-success radius size-L hidden">登录中...</span>
</div>
</div>
</form>
JS:
<script src="Scripts/jquery-1.11.3.js"></script>
<script src="Scripts/jquery.form.js" ></script>
<script src="Scripts/layer/layer.js"></script>
<script type="text/javascript">
function validate() {
if ($("#t_name").val() == "") {
layer.msg("请输入账户名!");
} else if ($("#t_pwd").val() == "") {
layer.msg("请输入密码!");
} else {
$("#form1").ajaxSubmit({
dataType: "json",
beforeSubmit: function () {
$("#b_submit").hide();
$("#loading").removeClass("hidden");
}, success: function (resp) {
if (resp.res == 1) { //登录成功
window.location.href = resp.data;
} else { //登录失败
$("#b_submit").show();
$("#loading").addClass("hidden");
$("#t_code").val("");
layer.msg(resp.msg);
}
}, error: function (e) {
$("#b_submit").show();
$("#loading").addClass("hidden");
}
});
}
}
//将光标定位到用户名输入框
window.onload = function () {
document.getElementById("t_name").focus();
}
</script>
C#后台(.aspx.cs文件)
protected void Page_Load(object sender, EventArgs e)
{
if (IsPost)
LoginUser();
}
private void LoginUser()
{
if (IsLocalRequest && !IsRepeatPost)
{
string loginname = GetFormString("t_name");
string loginpwd = GetFormString("t_pwd");
if (loginname == "")
ResponseText(Newtonsoft.Json.JsonConvert.SerializeObject(new { res = 0, msg = "请输入用户名!" }));
else if (loginpwd == "")
ResponseText(Newtonsoft.Json.JsonConvert.SerializeObject(new { res = 0, msg = "请输入密码!" }));
else
{
string str = Common.Utils.MD5(loginpwd.ToLower());
t_member model = MemberDal.m_MemberDal.GetModel(string.Format(" mb_name = '{0}' and mb_pwd='{1}'", loginname, Common.Utils.MD5(loginpwd.ToLower())));
if (model != null)
{
if (model.mb_islock == 1)
ResponseText(Newtonsoft.Json.JsonConvert.SerializeObject(new { res = 0, msg = "无法登录,该账户被锁定!" }));
else
{
model.mb_logintime = DateTime.Now.ToString();
model.mb_loginip = Request.UserHostAddress;
MemberDal.m_MemberDal.Update(model);
Response.Cookies[":member"].Value = Common.Serialize.SerializeObject(model);
Response.Cookies[":member"].Expires.AddDays(1);
ResponseText(Newtonsoft.Json.JsonConvert.SerializeObject(new { res = 1, msg = "登录验证通过", data = "/Master.aspx" }));
}
}
else {
ResponseText(Newtonsoft.Json.JsonConvert.SerializeObject(new { res = 0, msg = "账号或密码不正确!" }));
}
}
}
}