我是asp MVC的新手,所以我会尝试使用ajax创建一个登录表单,所以我写了jsonresult来检查控制器中的用户名和密码,我在ajax中调用它但是它不工作当我在日志消息登录中写下正确的用户名和密码时,我的控制器代码
public ActionResult login()
{
return View();
}
[HttpPost]
public JsonResult ValidateUser(string username,string password)
{
using(var contxt=new EnglisCenterEntities())
{
var data = from a in contxt.Employee
where a.Username == username && a.Passwords == password
select a;
if(data.Count()>0)
{
return Json(new { Success = true }, JsonRequestBehavior.AllowGet);
}
else
{
return Json(new { Success = false }, JsonRequestBehavior.AllowGet);
}
}
}
这是我的观点和ajax代码
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 alert alert-warning">
<h2 class="text-center">Login</h2>
<div class="row">
<div class="col-md-12">
<div class="form-group" id="log">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input type="text" id="username" class="form-control" placeholder="Username">
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
<input type="password" class="form-control" id="password" placeholder="Password">
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<button class="btn btn-warning form-control" type="submit" id="savedata"><i class="glyphicon glyphicon-log-in"></i> Login</button>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function () {
$('#log').submit(function (e) {
e.preventDefault();
var data = {
username: function () { return $("#username").val(); },
password: function () { return $("#password").val(); }
};
$.ajax({
url: "/Account/ValidateUser",
type: "Post",
data:data,
dataType: "json",
success: function (response) {
if (response.Success) {
$.get("@Url.Action("Index", "Home")", function (data) {
$('.container').html(data);
});
}
else
window.location.href = "@Url.Action("Login", "Account")";
},
error: function () {
console.log('Login Fail!!!');
}
});
});
});
</script>
那么为什么ajax代码没有检查用户名和密码?
答案 0 :(得分:0)
你应该使用
private void updateMenuTitles() {
if(isFavorite){
userItem.setIcon(new BitmapDrawable(getResources(), favoriteBitmap));
}else{
userItem.setIcon(new BitmapDrawable(getResources(), unFavoriteBitmap));
}
invalidateOptionsMenu();
}
答案 1 :(得分:0)
我的代码中可以看到2个错误:
var data = {
username: function() { return $("#username").val(); },
password: function() { return $("#password").val(); }
};
这将帮助您获得输入的实际值。
并且不要通过它来串通你的对象:
$.ajax({
//...
data: data,
//...
});
答案 2 :(得分:0)
如果要将模型值传递给控制器,则可以将值赋给变量并发布该变量。但是在这里,你在控制器中得到两个参数值。因此,您传递两个这样的参数,并将按钮类型更改为按钮:
<script>
$(document).ready(function () {
$('#savedata').click(function () {
$.ajax({
url: "/Account/ValidateUser",
type: "Post",
data:{"username": $("#username").val(),
"password": $("#password").val()
};,
dataType: "json",
contentType: "application/json",
success: function (response) {
if (response.Success) {
$.get("@Url.Action("Index", "Home")", function (data) {
$('.container').html(data);
});
}
else
window.location.href = "@Url.Action("Login", "Account")";
},
error: function () {
console.log('Login Fail!!!');
}
});
});
});
</script>
答案 3 :(得分:0)
伙计们,感谢您的回答以及您的帮助,这是我的代码
<script>
$(document).ready(function () {
$('#savedata').click(function (e) {
e.preventDefault();
var data = {
username: function () { return $("#username").val(); },
password: function () { return $("#password").val(); }
};
$.ajax({
url: "/Account/validateuser",
type: "POST",
data: data,
dataType: "json",
success: function (response) {
if (response.Success) {
$.get("@Url.Action("Index", "Home")", function (data) {
$('.container').html(data);
});
}
else
window.location.href = "@Url.Action("Login", "Account")";
},
error: function () {
console.log('Login Fail!!!');
}
});
});
});
答案 4 :(得分:0)
可能您可以将JQuery ajax更改为JQuery.get()
$.get('/Account/ValidateUser', { username: "xyx", password: "xyz"})
.success(function (response) {
if (response.Success) {
$.get("@Url.Action("Index", "Home")", function (data) {
$('.container').html(data);
});
}
else
window.location.href = "@Url.Action("Login", "Account")";
})
.fail(function () {
console.log('Login Fail!!!');
});