Adding a general parameter to all ajax calls made with jQuery

后端 未结 4 1093
离开以前
离开以前 2021-01-11 09:19

I\'m using AJAX to load data from the server as needed. I\'m currently working on updating the server software to the latest version. One of the things I noticed that has ch

4条回答
  •  时光取名叫无心
    2021-01-11 09:49

    I took Raja Khoury's solution, but edited it a bit since context was null in my case.

    This is what I came up with:

        $.ajaxPrefilter(function (options, originalOptions, jqXHR) {
            if (originalOptions.type === 'POST' || options.type === 'POST') {
                var modifiedData = $.extend({}, originalOptions.data, { __RequestVerificationToken: getAntiForgeryToken() });
                options.data = $.param(modifiedData);
            }
        });
    

提交回复
热议问题