【发布时间】:2016-02-23 09:21:35
【问题描述】:
我是 AngularJS 的新手。我想要的是从带有 $http post 的服务器获取令牌,然后使用来自请求的令牌用作授权标头,以便在其他页面中访问并跟踪对服务器的数据请求。这是我现有的代码:
var peopleApp = angular.module('peopleApp', ['ngRoute', 'ngAnimate']);
peopleApp.config(function($interpolateProvider, $httpProvider) {
// Change template tags
$interpolateProvider.startSymbol('[[');
$interpolateProvider.endSymbol(']]');
// Enabling CORS
$httpProvider.defaults.xsrfCookieName = 'csrftoken';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
// $httpProvider.defaults.withCredentials = true;
});
peopleApp.controller('formController', function($scope, $http, $location) {
$scope.logIn = function(json, url){
$http.post(url, json)
.then(function(response){
token = response.data.token;
window.location.href = 'crew-menu';
},function(response){
alert('Please check the following validations on the next alert and contact the creators regarding this error');
alert(JSON.stringify(response.data.errors));
});
}
});
附: 我知道这可以通过使用 .run 来完成,如下所示:
peopleApp.run(function($http) {
$http.defaults.headers.common.Authorization = 'YmVlcDpib29w';
});
但是令牌授权将来自通过发布请求的登录身份验证
【问题讨论】:
标签: angularjs