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

前端 未结 5 1641
时光取名叫无心
时光取名叫无心 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条回答
  •  Happy的楠姐
    2020-12-05 01:03

    Rather than manually adding it to each request, I usually do something like this:

    var token = $('input[name="__RequestVerificationToken"]').val();
    $.ajaxPrefilter(function (options, originalOptions) {
      if (options.type.toUpperCase() == "POST") {
        options.data = $.param($.extend(originalOptions.data, { __RequestVerificationToken: token }));
      }
    });
    

    This will automatically add your token to any ajax POST you do.

提交回复
热议问题