mar*_*aan 5 javascript cookies angularjs
我在AngularJS中使用$ http调用时遇到麻烦.我创建了以下服务
crApp.service('authService', function ($http) {
var urlBase = 'http://192.168.xx.xx:8081';
// POST api/login/
this.login = function (credentials) {
return $http.post(urlBase + "/api/login", credentials);
};
// POST api/logout/
this.logout = function () {
return $http.post(urlBase + "/api/logout/", "");
};
}); //end service
crApp.service('dataService', function ($http) {
var urlBase = 'http://192.168.xx.xx:8081';
// POST api/query/
this.pull = function (query) {
return $http.post(urlBase + "/api/query", query);
};
Run Code Online (Sandbox Code Playgroud)
从控制器我调用登录方法:
$scope.login = function(){
authService.login(credentials)
.success(function(data) {
console.log("RESULT LOGIN: " + data );
})
.error(function(data, status, headers, config) {
console.log(data);
console.log(status);
console.log(headers);
console.log(config);
});
};
Run Code Online (Sandbox Code Playgroud)
到目前为止一直很好,我收到了一个设置cookie的响应:

成功登录后,我调用以下方法:
var locations = { query: "from location select uid, name, description, address, phone", dataFormat: "verbose" };
$scope.getLocations = function() {
dataService.pull(portals)
.success(function (data) {
console.log("RESULT QUERY: " + data)
})
.error(function(data, status, headers, config) {
console.log("Query niet gelukt!");
console.log(data);
console.log(status);
console.log(headers);
console.log(config);
});
};
Run Code Online (Sandbox Code Playgroud)
结果:

所以cookie没有在标题中设置.我正在使用AngularJS 1.3.15并且对不同域上的远程服务器进行调用,但它在WebStorm(Jetbrains)中的REST客户端工具中可以正常工作.所以它一定是我在Angular中缺少的东西!??
希望有人可以帮助我.
Sco*_*ott 11
您需要withCredentials = true在$http配置中设置允许在CORS请求上设置cookie.本文解释了有关cookie和CORS的更多信息.
在您的应用配置中:
$httpProvider.defaults.withCredentials = true;
Run Code Online (Sandbox Code Playgroud)
例如:
angular.module('myApp', [])
.config(['$httpProvider'){
$httpProvider.defaults.withCredentials = true;
}]);
Run Code Online (Sandbox Code Playgroud)
您可以在$http提供程序文档中阅读有关此选项的信息.
| 归档时间: |
|
| 查看次数: |
12470 次 |
| 最近记录: |