Skip to content
Snippets Groups Projects
Commit 6142ea55 authored by Peter Rotich's avatar Peter Rotich
Browse files

Add CSRF header to ajax calls - .ajaxSetup moved from original class.csrf.php (jared's class)

parent 6c5d1e65
No related branches found
No related tags found
No related merge requests found
...@@ -173,7 +173,34 @@ $(document).ready(function(){ ...@@ -173,7 +173,34 @@ $(document).ready(function(){
/* global inits */ /************ global inits *****************/
//Add CSRF token to the ajax requests.
// Many thanks to https://docs.djangoproject.com/en/dev/ref/contrib/csrf/ + jared.
$(document).ajaxSend(function(event, xhr, settings) {
function sameOrigin(url) {
// url could be relative or scheme relative or absolute
var host = document.location.host; // host + port
var protocol = document.location.protocol;
var sr_origin = '//' + host;
var origin = protocol + sr_origin;
// Allow absolute or scheme relative URLs to same origin
return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
(url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
// or any other URL that isn't scheme relative or absolute i.e
// relative.
!(/^(\/\/|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", $("meta[name=csrf_token]").attr("content"));
}
});
/* Get config settings from the backend */ /* Get config settings from the backend */
$.get('ajax.php/config/ui.json', $.get('ajax.php/config/ui.json',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment