私はasp MVCで新しくなったので、私はajaxでログインフォームを作ろうと思っていますので、jsonresultにコントローラのユーザ名とパスワードをチェックして、それをajaxで呼びますが、それは動作しません。そのためには、私のコントローラのコードなぜ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">
<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>
</div>
<script>
$(document).ready(function() {
$('#savedata').click(function() {
var data = {
"username": $("#username").val(),
"password": $("#password").val()
};
$.ajax({
url: "/Account/ValidateUser",
type: "Post",
data: JSON.stringify(data),
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>
のですか?示すように任意のヘルプJSON.stringify
の必要はありません
ありますか?送信ボタンを使用しています。 – BenG
「機能していません」と定義します。それは何ですか? – David
あなたのajaxリクエストはエラーを投げますか? –