Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi -

Keeping it simple at first to start out with but not getting it to work. I'm trying to call code behind method using ajax. But my error that it is returning isnt descriptive and not sure what I'm doing wrong. Followed a few different articles and seems like I'm making it more difficult than it should be. Here is the code:

Here is the error : "{"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}"

Ajax
 $(document).ready(function () {
    $.ajax({
        url: "Registration.aspx/GetData",
        type:"POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            $('#responseContent').text(data.d);
        }

    });
});


Here is the code behind:
C#
[WebMethod]
public static string GetData()
{
    return "Code behind string";
}
Posted
Comments
Snesh Prajapati 3-Dec-15 0:48am    
In ajax method, you are using type:"POST" but you are not posting any data to GetData method. Verify in code whether you are making "GET" or "POST" call. Have a look at http://stackoverflow.com/questions/10653637/jquery-ajax-post-to-c-sharp
[no name] 3-Dec-15 1:10am    
Firstly it is throwing authentication failed. It means server is authenticating something(users or machine key) which is not valid.

InvalidOperationException occurs when a method call is invalid for the object's current state.

http://stackoverflow.com/questions/28038732/getting-authentication-failed-system-invalidoperationexception-error-intermit

http://stackoverflow.com/questions/23033614/asp-net-calling-webmethod-with-jquery-ajax-401-unauthorized
Troy Bryant 3-Dec-15 8:25am    
Actually the 3rd link pointed me in the right direction. Thank you
Why don't you just go through some links suggested by Google?
yeleswarapu ramakrishna 3-Dec-15 14:29pm    
code you have written works perfectly fine for me on my pc

1 solution

HTML
<script type="text/javascript">
      function dream() {
          $.ajax({
              url: "http://localhost:8080/Registration.aspx/GetData",
              type: "POST",
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function (data) {
                  $('#responseContent').text(data.d);
              }
          });
      }
      dream();
  </script>
 
Share this answer
 
v4

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