Access Aspx WebService method in Jquery Ajax

Today, I got the requirement where I need to call a method which is in Aspx from JavaScript and do some operation. Somehow, I felt it is very simple requirement and someone must have done this before. I did some search and there are many sources for it. This post is the combination of many posts…
Created a Web application.
Added a code within the method as shown below. Make sure you give [WebMethod].
public static string WebServiceCall(string Value)
    return “My name is: “ + Value;
Used below the Jquery code to call the method which was written in .aspx.cs page.

<script src=”Scripts/jquery-1.10.2.min.js”></script>

<script type=”text/javascript”>
        function CallWebMethod() {
                type: “POST”,
                url: “/Test.aspx/WebServiceCall”,
                data: ‘{Value: “‘ + $(“#<%=TextBox1.ClientID%>)[0].value + ‘” }’,
                contentType: “application/json; charset=utf-8”,
                dataType: “json”,
                success: OnSuccess,
                failure: function (response) {

        function OnSuccess(response) {

Added a HTML button in .aspx page and called the Jquery function.
    <form id=”form1″ runat=”server”>
        <asp:Label ID=”lblFirstName” runat=”server” Text=”Name”></asp:Label>
        <asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>
        <input id=”btnClick” type=”button” value=”Click Me!” onclick=”CallWebMethod(); />
When I execute the code and click on the button I got the exception as “Authentication Failed.”

 Fix for this : Goto — App_Start — RouteConfig and comment the below line as per the screen shot.

 Now the code should work properly.

Hope this helps.

Happy Coding

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s