The required anti-forgery form field __RequestVerificationToken is not present

user2010114 picture user2010114 · May 24, 2014 · Viewed 7.5k times · Source

Could you please help me to solve this issue? I'm implementing sign in form without form or html.beginform because I'm calling the controller /API through Ajax call and based on the status message / success, I want to redirect to some page as per the requirement. Please note, I shouldn't skip the anti-forgery method which has been implemented for the controller method. Thanks.

But I'm not able to proceed further because of "The required anti-forgery form field __RequestVerificationToken is not present"

My View is as follows :

""<input data-bind="value: UserName" type="text" name="username" />
  <button type="button" data-bind="click:LoginMethod"> Login       </button>""

And the Ajax code is as follows

 self.LoginMethod = function () {

        //Ajax call to Insert the Customer record

        $.ajax({

            type: "POST",

            url: "http://localhost:8089/Home/SignIn/",

            data: ko.toJSON(CustData), //Convert the Observable Data into JSON



            success: function (data) {
                console.log("success");
                alert("Record Added Successfully");

            },

            error: function () {
                console.log("failed");
                alert("Failed");

            }

        });

Appreciated your's help.

Answer

Claude JOASIL picture Claude JOASIL · Jan 2, 2015

Change data: ko.toJSON(CustData) to data: ko.toJS(CustData). And you should be good to go