Make Django and AngularJS play nicely together

03 Sep · by Tim Kamanin · 1 min read

Been working a lot with AngularJS and Django recently. To make them play nicely together one of the first things you need to do in your Angular App is to enable CSRF support and send X-Requested-With header, do it like this:

var app = angular.module('AppName', []);

app.config(['$httpProvider', function($httpProvider) {
  $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
  $httpProvider.defaults.xsrfCookieName = 'csrftoken';
  $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';}
]);

Since now on, all modules services and controllers where $http service is used, will send requests with csrf token and 'XMLHttpRequest' header.

Comments

Required for comment verification