From 752e8ccf66fe579503e53b77a7ca19ff67e3e4ef Mon Sep 17 00:00:00 2001 From: glaszig Date: Tue, 6 Aug 2019 22:41:29 +0200 Subject: [PATCH] improve global ajax event handling --- js/custom.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/js/custom.js b/js/custom.js index ccc2a2d8..dc58b2af 100644 --- a/js/custom.js +++ b/js/custom.js @@ -160,7 +160,14 @@ function setupBtns() { }); } -function updateCSRFToken(xhr, settings) { +function setCSRFTokenHeader(event, xhr, settings) { + var csrfToken = $('meta[name=csrf_token]').attr('content'); + if (/^(POST|PATCH|PUT|DELETE)$/i.test(settings.type)) { + xhr.setRequestHeader("X-CSRF-Token", csrfToken); + } +} + +function updateCSRFTokens(event, xhr, settings) { var newToken = xhr.getResponseHeader("X-CSRF-Token"); if (newToken) { $('meta[name=csrf_token]').attr('content', newToken); @@ -168,15 +175,9 @@ function updateCSRFToken(xhr, settings) { } } -$.ajaxSetup({ - beforeSend: function(xhr, settings) { - var csrfToken = $('meta[name=csrf_token]').attr('content'); - if (/^(POST|PATCH|PUT|DELETE)$/i.test(settings.type)) { - xhr.setRequestHeader("X-CSRF-Token", csrfToken); - } - }, - ajaxComplete: updateCSRFToken -}); +$(document) + .ajaxSend(setCSRFTokenHeader) + .ajaxComplete(updateCSRFTokens); $().ready(function(){ pageCurrent = window.location.href.split("?")[1].split("=")[1];