Skip to content

Commit 297bc51

Browse files
committed
Send csrf header for ajax requests
1 parent e45c2d6 commit 297bc51

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

fileupload/static/js/csrf.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// modify jquery ajax to add csrtoken when doing "local" requests
2+
$('html').ajaxSend(function(event, xhr, settings) {
3+
function getCookie(name) {
4+
var cookieValue = null;
5+
if (document.cookie && document.cookie != '') {
6+
var cookies = document.cookie.split(';');
7+
for (var i = 0; i < cookies.length; i++) {
8+
var cookie = jQuery.trim(cookies[i]);
9+
// Does this cookie string begin with the name we want?
10+
if (cookie.substring(0, name.length + 1) == (name + '=')) {
11+
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
12+
break;
13+
}
14+
}
15+
}
16+
return cookieValue;
17+
}
18+
if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
19+
// Only send the token to relative URLs i.e. locally.
20+
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
21+
}
22+
});

fileupload/templates/fileupload/picture_form.html

+1
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,5 @@ <h3 class="modal-title"></h3>
8484
<script src="{{ STATIC_URL }}js/jquery.fileupload-ui.js"></script>
8585
<script src="{{ STATIC_URL }}js/locale.js"></script>
8686
<script src="{{ STATIC_URL }}js/main.js"></script>
87+
<script src="{{ STATIC_URL }}js/csrf.js"></script>
8788
{% endblock %}

0 commit comments

Comments
 (0)