0

I'm trying to use Ajax to call a C# function but the call is not working.The script shows the hello message but does not show the success/error message.What am i doing wrong

Java script

<script type="text/javascript">
    $(document).ready(function () {
        $('#btnsave1').click(function () {
            alert("hello");
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "LeaveSurrender.aspx/apply",
                dataType: "json",
                success: function () {
                    alert('Successfully Saved');
                    // window.location.href = "ClubCreation.aspx";
                },
                Error: function () {
                    alert('error');
                }
            });

        });

    });

C# Method

 protected void apply()
    {
        MessageBox.Show("hi");
}

2 Answers 2

2

Try this:

   [WebMethod]//write [WebMethod]
   public static string apply()//method must be "pulic static" if it is in aspx page
   {
        return "Hi";
    }


 $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "LeaveSurrender.aspx/apply",
                dataType: "json",
                data:'{}',
                success: function (result) {
                    alert(result);
                    // window.location.href = "ClubCreation.aspx";
                },
                Error: function () {
                    alert('error');
                }
        });
Sign up to request clarification or add additional context in comments.

2 Comments

The way he is calling, the method neither have to be public nor have to be webmethod.
I forgot to add the public static,now works.But the MessageBox does not show and the objects which i refer in this method shows up an error saying that an object reference is required for non static fied,.....
2

Few things you need to fix here. First: there's no MessageBox in webforms. change the apply() method to return string:

protected string apply()
{
    return "hi!";
}

Second: Use '#btnsave1' to '#<%= btnsave1.ClientID %>' to get server generated id for button and also catch the string returned by apply() method. Your script should look like:

<script type="text/javascript">
    $(document).ready(function () {
        $('#<%= btnsave1.ClientID %>').click(function () {
            alert("hello");
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "LeaveSurrender.aspx/apply",
                dataType: "json",
                success: function (data) {
                    alert(data);
                    // window.location.href = "ClubCreation.aspx";
                },
                Error: function () {
                    alert('error');
                }
            });

        });

    });
</script>

Third: Make sure you have referenced jquery in the head of your page:

<head runat="server">
    <script src="Scripts/jquery-1.8.2.js"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.