js: Extract csrf.js and include in common bundle.

This should make it possible to use this AJAX setup code in logged-out
code as well, which is necessary to use blueslip from portico pages.
This commit is contained in:
Tim Abbott
2018-12-16 15:15:52 -08:00
parent ed9430f3a6
commit adebe1bd4e
4 changed files with 17 additions and 14 deletions

15
static/js/csrf.js Normal file
View File

@@ -0,0 +1,15 @@
var csrf_token;
$(function () {
// This requires that we used Jinja2's {% csrf_input %} somewhere on the page.
csrf_token = $('input[name="csrfmiddlewaretoken"]').attr('value');
window.csrf_token = csrf_token;
$.ajaxSetup({
beforeSend: function (xhr, settings) {
if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
// Only send the token to relative URLs i.e. locally.
xhr.setRequestHeader("X-CSRFToken", csrf_token);
}
},
});
});