Make Django and AngularJS play nicely together

Sep 03, 2014 · Updated: Jul 05, 2021 · 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:

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.

Hey, if you've found this useful, please share the post to help other folks find it:

There's even more:

Subscribe for updates

  • via Twitter: @timonweb
  • old school RSS:
  • or evergreen email ↓ ↓ ↓