skip to Main Content

I am trying to call a WebMethod using AJAX in ASP.net

When debugging, I encountered the following error

{Message: "Authentication failed.", StackTrace: null,…}
ExceptionType: "System.InvalidOperationException"
Message: "Authentication failed."
StackTrace: null

AJAX:

                                <script type="text/javascript">  

                                    function SaveRecord() {
                                        //Get control's values  

                                        var year = $("#yeartxt").val();
                                        var title = $("#titletxt").val();
                                        var content = $("#contenttxt").val();

                                        //Jquery Ajax call to server side method  
                                        $.ajax({
                                            type: "POST",
                                            url: "Page.aspx/InsertDetails",
                                            data: "{'Year':" + year + ",'Title':" + title + ",'Content':" + content + "}",
                                            contentType: "application/json; charset=utf-8",
                                            dataType: "json",
                                            success: function (data) {
                                                alert("Updated!");
                                            }
                                        });

                                    }
                                </script>  

WebMethod:

        [WebMethod]
        public static string InsertDetails(string Year, string Title, string Content)

RouteConfig:

            var settings = new FriendlyUrlSettings();
            settings.AutoRedirectMode = RedirectMode.Permanent;
            routes.EnableFriendlyUrls(settings);

WebConfig:

     <authentication mode="Forms">
        <forms loginUrl="./SignIn.aspx"></forms>
      </authentication>

I implemented the form authentication using ASP.net Identity.

Any ways to solve this?

2

Answers


  1. It is probably your data object which is not well defined.

    const data = {'Year':year,'Title': title, 'Content': content};
    

    abd then in your request:

    ...
    data: JSON.stringify(data),
    ...
    

    THis should work, as you have mentioned the data type to be json

    Login or Signup to reply.
  2. Try this code

     $.ajax({
                                            type: "POST",
                                            url: "Page.aspx/InsertDetails",
                                            data: JSON.stringify({ 'Year': Year, 'Title': Title, 'Content': content  }),
                                            contentType: "application/json; charset=utf-8",
                                            dataType: "json",
                                            success: function (data) {
                                                alert("Updated!");
                                            }
                                        });
    

    RouteConfig:

    Comment this code

    //settings.AutoRedirectMode = RedirectMode.Permanent;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search