include antiforgerytoken in ajax post ASP.NET MVC

后端 未结 11 2421
感动是毒
感动是毒 2020-11-22 14:00

I am having trouble with the AntiForgeryToken with ajax. I\'m using ASP.NET MVC 3. I tried the solution in jQuery Ajax calls and the Html.AntiForgeryToken(). Using that solu

11条回答
  •  情书的邮戳
    2020-11-22 14:31

    Feel free to use the function below:

    function AjaxPostWithAntiForgeryToken(destinationUrl, successCallback) {
    var token = $('input[name="__RequestVerificationToken"]').val();
    var headers = {};
    headers["__RequestVerificationToken"] = token;
    $.ajax({
        type: "POST",
        url: destinationUrl,
        data: { __RequestVerificationToken: token }, // Your other data will go here
        dataType: "json",
        success: function (response) {
            successCallback(response);
        },
        error: function (xhr, status, error) {
           // handle failure
        }
    });
    

    }

提交回复
热议问题