开发者

How are people handling AJAX posts to ActionMethods using [ValidateAntiForgeryToken]

Seeing that the __RequestVerificationToken is not sent when using AJAX and ValidateAntiForgeryTokenAttribute is looking for the token in Request.Form, how are people dealing with this problem.

I ended up doing this.

$("#regmember-form").submit(function (e) {
    e.preventDefault();

    var token = $('[name="__RequestVerificati开发者_StackOverflow社区onToken"]').val();

    alert($(this).attr('action'));

    $.ajax({
        url: $(this).attr('action'),
        type: $(this).attr('method'),
        data: { __RequestVerificationToken: token }
    });

    return false;
});

Very similar to the accepted answer.


I grab the input off the page and send it back with the form post. This assumes that you include it on the page in the first place.

 $('#somebutton').click( function() {
     var data = $('[name="__RequestVerificationToken"]').serialize();
     $.post('/foo/bar', data, function(result) {
        // ...
     });
 });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜