include antiforgerytoken in ajax post ASP.NET MVC

后端 未结 11 2420
感动是毒
感动是毒 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:39

    
    
            function DeletePersonel(id) {
    
                    var data = new FormData();
                    data.append("__RequestVerificationToken", "@HtmlHelper.GetAntiForgeryToken()");
    
                    $.ajax({
                        type: 'POST',
                        url: '/Personel/Delete/' + id,
                        data: data,
                        cache: false,
                        processData: false,
                        contentType: false,
                        success: function (result) {
    
                        }
                    });
    
            }
        
    
            public static class HtmlHelper
            {
                public static string GetAntiForgeryToken()
                {
                    System.Text.RegularExpressions.Match value = System.Text.RegularExpressions.Regex.Match(System.Web.Helpers.AntiForgery.GetHtml().ToString(), "(?:value=\")(.*)(?:\")");
                    if (value.Success)
                    {
                        return value.Groups[1].Value;
                    }
                    return "";
                }
            }
    

提交回复
热议问题