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 ..开发者_Python百科 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.
$.post({
url: '@Url.Action("CheckLogin","Home")'
data: postdata,
success: function (msg) {
$('#Result').html(msg);
}
});
if doing this only not working, try changing your action like this,
public string CheckLogin( string Email, string Password){
}
and change your postData,
var postdata =
{
Email: username,
Password: password
};
Your post leaves me with more questions than answers. I would spend some time debugging ...
- Is your website running on port 7651?
- is CheckLogin in your Home controller?
- Are your routes the default routes, or what's your mapping like?
- if you use net panel in FireBug do you see a request go out? What's the result of call?
- Whats the "Users" model look like (could be a binding error)?
- Have you put a breakpoint in an OnException method in your controller ?
- Does #Result exist in your DOM (try an alert in the successfunction)?
- have you tried adding the "error" property to your ajax call and interrogating the issue?
Your controller action is returning a string not an ActionResult which in this case would be a ContentResult.
Try:
public ContentResult CheckLogin(Users checkuser)
{
if (db.CheckUserLoginDetails(checkuser.Email, checkuser.Password))
{
return Content("Login Successful:" + checkuser.Email);
}
else
{
return Content("Login UnSuccessful");
}
}
精彩评论