开发者

Trying to Login through AJAX Asp.net - MVC 3 - Not working

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...

  1. yes i am runing on localhost
  2. CheckLogin action is i Home Controller
  3. Routes are defualt...
  4. will try to have a look on net panel.. dont have any idea on that
  5. 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; }
    
  6. i have tried the breakpoints but the calls are not going and breakpoints are never hitting...

  7. Result DIV exits in DOM ..开发者_Python百科 in index view/ in HTML.BeginForm()
  8. 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 ...

  1. Is your website running on port 7651?
  2. is CheckLogin in your Home controller?
  3. Are your routes the default routes, or what's your mapping like?
  4. if you use net panel in FireBug do you see a request go out? What's the result of call?
  5. Whats the "Users" model look like (could be a binding error)?
  6. Have you put a breakpoint in an OnException method in your controller ?
  7. Does #Result exist in your DOM (try an alert in the successfunction)?
  8. 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");
        }
    }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜