0

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.

tereško
  • 56,151
  • 24
  • 92
  • 147
user2010114
  • 1
  • 1
  • 6
  • you have ``ValidateAntiFrogeryToken`` Attribute on your action thats why this error is coming – Ehsan Sajjad May 24 '14 at 18:30
  • 2
    Out of the box, the `AntiForgery` feature of MVC only works on traditional form posts. To get it to also work with ajax, the feature needs to be extended. There are a number of good blog posts with examples on how to do this that can be used as a foundation for your own implementation. [Here](http://richiban.wordpress.com/2013/02/06/validating-net-mvc-4-anti-forgery-tokens-in-ajax-requests/) and [here](http://haacked.com/archive/2011/10/10/preventing-csrf-with-ajax.aspx/) are good starting points. – Bryan May 24 '14 at 19:00
  • possible duplicate of [include antiforgerytoken in ajax post ASP.NET MVC](http://stackoverflow.com/questions/14473597/include-antiforgerytoken-in-ajax-post-asp-net-mvc) – CodeCaster May 24 '14 at 19:51

1 Answers1

0

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