function getCOOKIE(name) { var COOKIEValue = null; if (document.COOKIE && document.COOKIE !== '') { var COOKIEs = document.COOKIE.split(';'); for (var i = 0; i var COOKIE = jQuery.trim(COOKIEs[i]); // Does this COOKIE string begin with the name we want? if (COOKIE.substring(0, name.length + 1) === (name + '=')) { COOKIEValue = decodeURIComponent(COOKIE.substring(name.length + 1)); break; } } } return COOKIEValue; } var csrftoken = getCOOKIE('csrftoken'); function csrfSafeMethod(method) { // these HTTP methods do not require CSRF protection return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)); } $.ajaxSetup({ beforeSend: function (xhr, settings) { if (!csrfSafeMethod(settings.type) && !this.crossDomain) { xhr.setRequestHeader("X-CSRFToken", csrftoken); } } });