<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>登录</title>
<link href="Content/login.css" rel="stylesheet" type="text/css" />
<link type="text/css" href="jqueryui/css/smoothness/jquery-ui-1.8.20.custom.css" rel="stylesheet" />
<script type="text/javascript" src="Script/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var $username = $("#txt_username");
var $userpwd = $("#txt_userpwd");
var $logincode = $("#logincode");
var $info = $("#lbl_info");
var $remeberpwd = $(":checkbox[name='remeberpwd']").attr("checked") == true ? "1" : "0";
$("#btn_login").click(function () {
if (checkinput() == true) {
$.ajax({
type: "post",
url: "ajax/MyLoginHandler.ashx",
data: "{'username':'" + $username.val() + "','userpwd':'" + $userpwd.val() + "','remeberpwd':'" + $remeberpwd + "','logincode':'" + $logincode.val() + "'}",
success: function (msg) {
alert(1);
},
error: function () {
alert(2);
// showvalidatecode();
}
});
}
return false;
}); $("#getcode_img").click(function () {
showvalidatecode();
});
function showvalidatecode() {
$("#getcode_img").attr("src", "Control/validate.aspx");
}; function checkinput() {
if ($username.val().length == 0) {
$info.text("用户名为空");
return false;
}
if ($userpwd.val().length == 0) {
$info.text("密码为空");
return false;
}
//加上js正则
return true;
};
});
</script>
</head> public class MyLoginHandler : IHttpHandler,IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
string username = context.Request.Form["username"];
string userpwd = context.Request.Form["userpwd"];
string remeberpwd =context.Request.Form["remeberpwd"];
string logincode = context.Request.Form["logincode"];
if (logincode == context.Session["logincodenum"].ToString())
{
if (new LoginHandler().ValidateUser(username, userpwd))
{
if (remeberpwd == "1")
{ }
context.Response.Write("3" + "|登陆成功,进行跳转");
}
else
{
context.Response.Write("2" + "|用户名或密码错误");
}
}
else
{
context.Response.Write("1" + "|验证码错误");
} }
实在找不出为什么错了 200分吧! 求助
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>登录</title>
<link href="Content/login.css" rel="stylesheet" type="text/css" />
<link type="text/css" href="jqueryui/css/smoothness/jquery-ui-1.8.20.custom.css" rel="stylesheet" />
<script type="text/javascript" src="Script/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var $username = $("#txt_username");
var $userpwd = $("#txt_userpwd");
var $logincode = $("#logincode");
var $info = $("#lbl_info");
var $remeberpwd = $(":checkbox[name='remeberpwd']").attr("checked") == true ? "1" : "0";
$("#btn_login").click(function () {
if (checkinput() == true) {
$.ajax({
type: "post",
url: "ajax/MyLoginHandler.ashx",
data: "{'username':'" + $username.val() + "','userpwd':'" + $userpwd.val() + "','remeberpwd':'" + $remeberpwd + "','logincode':'" + $logincode.val() + "'}",
success: function (msg) {
alert(1);
},
error: function () {
alert(2);
// showvalidatecode();
}
});
}
return false;
}); $("#getcode_img").click(function () {
showvalidatecode();
});
function showvalidatecode() {
$("#getcode_img").attr("src", "Control/validate.aspx");
}; function checkinput() {
if ($username.val().length == 0) {
$info.text("用户名为空");
return false;
}
if ($userpwd.val().length == 0) {
$info.text("密码为空");
return false;
}
//加上js正则
return true;
};
});
</script>
</head> public class MyLoginHandler : IHttpHandler,IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
string username = context.Request.Form["username"];
string userpwd = context.Request.Form["userpwd"];
string remeberpwd =context.Request.Form["remeberpwd"];
string logincode = context.Request.Form["logincode"];
if (logincode == context.Session["logincodenum"].ToString())
{
if (new LoginHandler().ValidateUser(username, userpwd))
{
if (remeberpwd == "1")
{ }
context.Response.Write("3" + "|登陆成功,进行跳转");
}
else
{
context.Response.Write("2" + "|用户名或密码错误");
}
}
else
{
context.Response.Write("1" + "|验证码错误");
} }
实在找不出为什么错了 200分吧! 求助
string username = context.Request.Form["username"];
string userpwd = context.Request.Form["userpwd"];
string remeberpwd =context.Request.Form["remeberpwd"];
string logincode = context.Request.Form["logincode"];
这么做是不行滴,请参考:
data: "co="+$.trim(str.val())+"&wd="+$.trim(pwd.val())+"&kp="+keepPW+"&type=usual&way="+$("#hidtype").val()+"&acDate="+(new Date().getTime()),
红色部分都不要单引号
data: "{username:$.trim($("#txt_username").val())}",
error: function (a, s, d) {
alert(s + " " + d);
}看看弹出来的是什么错误
data:{"username":$username.val(),"userpwd":$userpwd.val(),"remeberpwd":$remeberpwd.val(),"logincode":$logincode.val()}
既然断点都进去了,那么ashx的路径应该是没问题的
估计就像LS说的 是参数格式的问题
data:{"key1":value1,"key2":value2} 这样的方式写
就是19L的写法LZ可以去用Firbug调试一下,看看在请求过去的URL上面有没有带着 参数,还有看下参数的格式是否正确
试一试这个。
还有你的html代码没贴出来
type: "GET",
data: { zyid: zyid , zyname:zyname},
datatype: "json",
success: function(data, status) {
}
data的格式写成上面的格式,设置断点或alert看前台要传递的数据有没有取到!
ashx中没有取得数据应该是前台提交的时候没有提交成功数据!!ashx中取数据写成:context.Request.QueryString["zyid"]试试!
Hander页面返回值了没有???
首先把大括号外面的引号要去掉
换成直接用jquery的post方法就能正常提交取到
用ajax就不行
data: {username:$username.val(),userpwd:$userpwd.val(),remeberpwd:$remeberpwd,logincode:$logincode.val() }
取的时候
context.Request.Params["username"];
这个该是个老问题了
如下方式可以得到数据
http://stackoverflow.com/questions/2948628/asp-net-passing-json-from-jquery-to-ashx/8714375#8714375
http://stackoverflow.com/questions/11269622/asp-net-jquery-ajax-json-simple-example-of-exchanging-data