Make Django and AngularJS play nicely together

Sep 03, 2014 · by Tim Kamanin

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:

javascript
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.

Want to get more 🔥 tips like this one?

Subscribe to get notified about new dev tutorials