Last active
September 27, 2016 10:11
-
-
Save kayzhu/4714734 to your computer and use it in GitHub Desktop.
Send POST request in Angular.js to a CSRF-protected Django view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript" src="/angular/angular.js"></script> | |
<script type="text/javascript" src="/angular/angular-cookies.min.js"></script> | |
// app.js | |
// inject ngCookies to your app named 'myApp'. | |
angular.module('myApp', ['ngCookies']); | |
// controller.js | |
function MyCtrl($scope, $http, $cookies) { | |
var my_url = $scope.url; | |
var my_data = $scope.my_data; | |
$http({ | |
url: my_url, | |
data: my_data, | |
method: 'POST', | |
headers: { | |
'X-CSRFToken': $cookies['csrftoken'] | |
} | |
}). | |
success(function(data) { | |
console.log(data); | |
}). | |
error(function(data) { | |
console.log(data); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment