设置全局的csrf跨站请求伪造

function csrfSafeMethod(method) {
        // these HTTP methods do not require CSRF protection
        return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
    }

    $.ajaxSetup({
        beforeSend: function (xhr, settings) {
            // 请求头中设置一次csrf-token
            if(!csrfSafeMethod(settings.type)){
                xhr.setRequestHeader("X-CSRFToken", $.cookie('csrftoken'));
            }
        }
    });



在后面的所有的ajax请求均不用再设置csrf跨站请求伪造,因为在ajax发送前都会执行以上代码。已经做好csrf跨站请求伪造。(以上代码来自官方文档)

  

原文地址:https://www.cnblogs.com/guomeina/p/7678645.html