Click here to Skip to main content
15,911,528 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys,

again i need your help. i'm done deploying my MVC web application. but i have another problem encounter. my ajax did not work. my ajax inside my javascript. please see below code.
<script type="text/javascript">
    $(document).ready(function () {
        $('#btnLogin').click(function () {
            var tmplogin = { "tmpUName": "", "tmpPword": "" };

            tmplogin.tmpUName = document.getElementById('txtUserID').value;
            tmplogin.tmpPword = document.getElementById('txtUserPassword').value;

            $.ajax({
                url: '/Home/Verify',
                data: JSON.stringify(tmplogin),
                type: 'POST',
                contentType: 'application/json;',
                dataType: 'json',
                success: function (result) {

                    if (result.Success != 0) {
                        window.location.href = "/AdminPage/Index"
                    }
                    else {
                        alert(result.ex);
                    };
                }
            });
        });
    });
</script>


and my controller is:
SQL
[HttpPost]
public JsonResult Verify(string tmpUName, string tmpPword)
{
    try
    {
       return Json(new { Success = 0, ex = "Success" });
    }
    catch (Exception ex)
    {
        return Json(new { Success = 0, ex = ex.Message.ToString() });
    }
}


as simple as that.. yet it won't return any message.
i would believe that problem is in ajax because when i try to put an alert message after

C#
tmplogin.tmpUName = document.getElementById('txtUserID').value;
tmplogin.tmpPword = document.getElementById('txtUserPassword').value;

alert("alert");


the alert messge will pop-up.

any idea how to resolve this? is there any dll or files needed to add on my reference?

thanks in advance.

-naijeru-
Posted

If you are using ASP.Net TextBox controls for input fields use ClientID of both the textboxes in javascript.

change your code to:
C#
tmplogin.tmpUName = document.getElementById('<%=txtUserID.ClientID %>').value;
tmplogin.tmpPword = document.getElementById('<%=txtUserPassword.ClientID %>').value;

Otherwise there shouldn't be any issue with your code.
Hope this helps.
 
Share this answer
 
v2
instead of
url: '/Home/Verify',

i changed to
url: '@Url.Action("Verify","Home")',


same as
window.location.href = "/AdminPage/Index"

changed into
window.location.href = '@Url.Action("Index","AdminPage")'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900