Javascript 如何更改资源';angularjs中的s头
我试图设置资源的标题(下面的代码)。 当我实例化我的资源(Javascript 如何更改资源';angularjs中的s头,javascript,angularjs,http-headers,Javascript,Angularjs,Http Headers,我试图设置资源的标题(下面的代码)。 当我实例化我的资源($scope.user=new rsrUser;)时,angularjs会获取尚未定义的cookie(从“getHMAC()”内部触发“undefined”错误)。只有在触发“$scope.login()”时(当用户单击界面中的按钮时)才会定义cookie 有更好的方法吗 controllers.js angularjsWebInterfaceControllers.controller('loginCtrl', ['$scope', '
$scope.user=new rsrUser;
)时,angularjs会获取尚未定义的cookie(从“getHMAC()”内部触发“undefined”错误)。只有在触发“$scope.login()”时(当用户单击界面中的按钮时)才会定义cookie
有更好的方法吗
controllers.js
angularjsWebInterfaceControllers.controller('loginCtrl', ['$scope', 'rsrUser',
function($scope, rsrUser){
$cookieStore.put("username","therebedragons");
$cookieStore.put("password","therebedragons");
$scope.user = new rsrUser;
$scope.user.username = ""; //bound to input field in interface
$scope.user.password = ""; //bound to input field in interface
$scope.login = function() {
$cookieStore.put("username", $scope.user.username);
$cookieStore.put("password", $scope.user.password);
$cookieStore.put("state", "loggedOUT");
$scope.user.$logIn(
function(){
$cookieStore.put("state", "loggedIN");
}, function() {
$cookieStore.put("username","therebedragons");
$cookieStore.put("password","therebedragons");
$cookieStore.put("state", "loggedOUT");
}
)
};
}]);
services.js
angularjsWebInterfaceServices.service('rsrUser', [ '$resource', '$cookieStore',
function($resource, $cookieStore){
var req = "/login"
var timestamp = getMicrotime(true).toString();
var username = $cookieStore.get("username");
var key = $cookieStore.get("password");
return $resource(baseURL + req, {}, {
logIn: {method:'POST',
isArray:false,
headers:{
'X-MICROTIME': timestamp,
'X-USERNAME': username,
'X-HASH': getHMAC(username,timestamp,req,key)
}
}
});
}]);
编辑:实际上,一旦控制器被实例化,cookies就会被定义 头的值可以是返回字符串的函数(请参见此处的参数:)。这样,在调用
logIn
方法之前,不会在资源中访问cookie
return $resource(baseURL + req, {}, {
logIn: {method:'POST',
isArray:false,
headers: {
'X-MICROTIME': timestamp,
'X-USERNAME': function() {
return $cookieStore.get("username");
},
'X-HASH': function() {
var username = $cookieStore.get("username");
return getHMAC(username,timestamp,req,key)
}
}
}
});