Set the X-CSRF-Token header for AJAX requests with jQuery. #950

This commit is contained in:
Andrew Smith 2012-03-25 08:45:48 +01:00 committed by Felix Schäfer
parent 375045a82b
commit 61c00779d0
1 changed files with 9 additions and 1 deletions

View File

@ -467,7 +467,15 @@ jQuery.viewportHeight = function() {
// Automatically use format.js for jQuery Ajax
jQuery.ajaxSetup({
'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
'beforeSend': function(xhr) {
xhr.setRequestHeader("Accept", "text/javascript");
// TODO: Remove once jquery-rails (Rails 3) has been added a dependency
var csrf_meta_tag = jQuery('meta[name="csrf-token"]');
if (csrf_meta_tag) {
xhr.setRequestHeader('X-CSRF-Token', csrf_meta_tag.attr('content'));
}
}
})
/* TODO: integrate with existing code and/or refactor */