Ajax server method call not working.

Adeel Mirza 21 Reputation points
2023-01-31T09:24:28.8533333+00:00

I am using this code to call the server side method using Ajax.



<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        function LoginUser() {
            $.ajax({
                type: "POST",
                url: "Login.aspx/LoginUser",
                data: '',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: alert("IN")
            });
        }
        //}
        //function OnSuccess(response) {
        //    alert(response.d);
        //}
    </script>

<input id="btnLogin" type="button" value="Login"
                                            onclick="LoginUser()" />

Now at Code Behind. But on Button Press the method isn't getting called. What could be the reason



[System.Web.Services.WebMethod]
    public void LoginUser()
{
     ------------------------
     ------------------------
     ------------------------
}

    
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,597 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 30,176 Reputation points Microsoft Vendor
    2023-01-31T09:59:08.08+00:00

    Hi @Adeel Mirza ,

    You can check the specific error message through the F12 developer tool.

    First of all your jquery file is invalid, you can download it through NuGet Package Manager, or directly use the link below.

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>

    Second, when you call the method from JavaScript, you need to use a static instance.

            [System.Web.Services.WebMethod]
            public static void LoginUser()
            {
                
            }
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.