asp.net mvc - Ajax post throws A required anti-forgery token was not supplied or was invalid -
i'm trying secure ajax posts using anti-forgery mechanism.
first i've added antiforgerytoken helper method call view
@html.antiforgerytoken() and adjusted jquery post call
var values = $(this).serialize() + "&__requestverificationtoken=" + $("input[name='__requestverificationtoken']").val(); $.post(url, values) .success(page.submitsuccess) .error(page.submiterror) .complete(page.submitcomplete); and of course decorated action method validateantiforgerytoken
[httppost] [validateantiforgerytoken] public actionresult create(projectcreatecommand command) { .... } but after submiting form throws a required anti-forgery token not supplied or invalid error.
i've deleted token cookie , i've restarted browser.
am missing ?
if use $(form).serialize(); serialize input tags, can $.post jquery method. try this:
$("#you_form").submit(function (e) { e.preventdefault(); $.ajax({ type: 'post', url: $(this).attr("action"), // url action attribute, route setted html.beginform() data: $(this).serialize(), // serialize input tags form success: page.submitsuccess; }); }); there no problem using @html.antiforgerytoken(), serialize method add together.
take @ link: http://api.jquery.com/jquery.post/
Comments
Post a Comment