$(document).ajaxSend(
function
(event, xhr, settings) {
function
getCookie(name) {
var
cookieValue =
null
;
if
(document.cookie && document.cookie != &
#39;') {
var
cookies = document.cookie.split(&
#39;;');
for
(
var
i = ; i < cookies.length; i++) {
var
cookie = jQuery.trim(cookies[i]);
if
(cookie.substring(, name.length + ) == (name + &
#39;=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + ));
break
;
}
}
}
return
cookieValue;
}
function
sameOrigin(url) {
var
host = document.location.host;
var
protocol = document.location.protocol;
var
sr_origin = &
#39;//' + host;
var
origin = protocol + sr_origin;
return
(url == origin || url.slice(, origin.length + ) == origin + &
#39;/') ||
(url == sr_origin || url.slice(, sr_origin.length + ) == sr_origin + &
#39;/') ||
!(/^(\/\/|http:|https:).*/.test(url));
}
function
safeMethod(method) {
return
(/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
if
(!safeMethod(settings.type) && sameOrigin(settings.url)) {
xhr.setRequestHeader(
"X-CSRFToken"
, getCookie(&
#39;csrftoken'));
}
});