Antiforgerytoken meesturen met form

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(string someValue)
{
  return Json(new { someValue = someValue });
}@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "__AjaxAntiForgeryForm" }))
{
    @Html.AntiForgeryToken()
}
<script type="text/javascript">
        var form = $('#__AjaxAntiForgeryForm');
        var token = $('input[name="__RequestVerificationToken"]', form).val();
        $.ajax({
            url: $(this).data('url'),
            type: 'POST',
            data: { 
                __RequestVerificationToken: token, 
                someValue: 'some value' 
            },
            success: function (result) {
                alert(result.someValue);
            }
        });
        return false;

</script>

 

Categories: , ,

Related Article