Click here to Skip to main content
14,937,132 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am writing one jquery ajax call in my application but it was not trigger in my view
I could not able to find the solution for this issue.

Js Code:-

var producer = "test";
var json = { "EmailId": producer};
$.ajax({
type: "POST",
url: "/Account/ForgotPassword/",
data: json,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert("okay");
},

error: function (data) {
alert("not okay");
}
});

Controller:-

SQL
[HttpPost]
       [AllowAnonymous]
      [ValidateAntiForgeryToken]
       public ActionResult ForgotPassword(string EmailId)
       {
         return view();
       }
Posted

1 solution

For validating anti forgery token with in ajax call, you need to do a slight change.

Below is my Index page.
HTML
<form id="__AjaxAntiForgeryForm" action="#" method="post">

     @Html.AntiForgeryToken()
    <button onclick="test()">
        ClickMe
    </button>
   
</form>


Now keeping your Javascript code with slight modification as below.

JavaScript
<script>
       function test() {

           AddAntiForgeryToken = function (data) {
               data.__RequestVerificationToken = $('#__AjaxAntiForgeryForm input[name=__RequestVerificationToken]').val();
               return data;
           };
           var producer = "test";
           var json = AddAntiForgeryToken( { "EmailId": producer });
           $.ajax({
               type: "POST",
               url: "/Employee/ForgotPassword/",
               data: json,
               dataType: "json",
               success: function (data) {
                   alert("okay");
               },

               error: function (data) {
                   alert("not okay");
               }
           });


       }


   </script>


Basically here you have to pass the data with token. The main changes on data parameter.

And below is the action method in my Employee controller instead of Account controller.

C#
[HttpPost]
       [AllowAnonymous]
       [ValidateAntiForgeryToken]
       public ActionResult ForgotPassword(string EmailId)
       {
           return View();
       }


Hope this helps.

I am able to call action method by doing above changes on your code.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900