i’ve been trying to login through AJAX but somehow its not working.
my controller action is
public string CheckLogin(Users checkuser)
{
if (db.CheckUserLoginDetails(checkuser.Email, checkuser.Password))
{
return "Login Successful:" + checkuser.Email;
}
else
{
return "Login UnSuccessful";
}
}
my View AJAX code is
$(':submit').click(function (e) {
var username = $('#username').val();
var password = $('#password').val();
var postdata =
{
'Email': username,
'Password': password
};
$.post({
url: 'http://localhost:7651/Home/CheckLogin',
data: postdata,
success: function (msg) {
$('#Result').html(msg);
}
});
});
i don’t know whats wrong in the code ..but some how its not making a call to controller action at all…
- yes i am runing on localhost
- CheckLogin action is i Home Controller
- Routes are defualt…
- will try to have a look on net panel.. dont have any idea on that
-
USERs Model
[Key] public virtual int UserID { get; set; } [Required(ErrorMessage="Required")] public virtual string FirstName { get; set; } [Required(ErrorMessage = "Required")] public virtual string LastName { get; set; } [Required(ErrorMessage = "Required")] [DataType(DataType.EmailAddress)] public virtual string Email { get; set; } [Required(ErrorMessage = "Required")] [DataType(DataType.Password)] public virtual string Password { get; set; } [DataType(DataType.Date)] public virtual DateTime JoiningDate { get; set; } -
i have tried the breakpoints but the calls are not going and breakpoints are never hitting…
- Result DIV exits in DOM .. in index view/ in HTML.BeginForm()
- dont know how to add error to $.AJAX
Thanks for all the checklist though…. please help
here is the view …
@model Temp1.Models.Users
@{
ViewBag.Title = "Index";
}
<script src="@Url.Content("~/Scripts/jquery-1.5.1.js") type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-1.5.1-vsdoc.js") type="text/javascript"></script>
<h2>Login</h2>
@using (Html.BeginForm("CheckLogin", "Home"))
{
@*<p>
<input type="text" id="username" value="" />
</p>
<p>
<input type="password" id="password" value="" />
</p>
*@
@Html.TextBoxFor(model => model.Email)
@Html.PasswordFor(model => model.Password)
<p>
<input type="button" value="Login" id="btnLogin" />
</p>
<div id="Result">
</div>
}
<script type="text/javascript">
$('#btnLogin').click(function (e) {
var postdata =
{
"Email": "temp@temp.com",
"Password": "temp123"
};
$.ajax({
url: '@Url.Action("CheckLogin","Home")',
data: postdata,
success: function (msg) {
$('#Result').html(msg);
},
error: function (data) {
$('#Result').html(msg);
}
});
});
</script>
Your ajax submit url should be like this ‘/controler/action’. The best practice is to use
Url.Action("actionName","ControlerName")if not you will have to change your jQuery when you deploy somewhere else.if doing this only not working, try changing your action like this,
and change your postData,