How to make ajax request with anti-forgery token in mvc

前端 未结 5 1638
时光取名叫无心
时光取名叫无心 2020-12-05 00:47

I have problem with below details from MVC project.

When I am trying to use jquery ajax request with loading panel like spinning gif (or even text), I am getting err

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 00:48

    Have you added the token to your View? Like this:

    @Html.AntiForgeryToken()

    Since your controller receiving the post is looking for the anti forgery token, you need to ensure that you add it to your form in the view.

    EDIT:

    Try building your data in json first:

    var formData = $('form[action="/TransportJobAddress/Create"]').serialize();
    $.extend(formData, {'__RequestVerificationToken': token });
    
    //and then in your ajax call:
    $.ajax({
        //...
        data:formData
        //...
    });
    

提交回复
热议问题