AJAX Authentication Failed

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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;

Method 2

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x