AngularJS - 访问HTTP头 [英] AngularJS - accessing http headers
本文介绍了AngularJS - 访问HTTP头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图访问我的角度控制器中的HTTP头,但我越来越不确定。另外,我能看到我的角度服务的响应头这是不是在我的控制器反映。有人可以告诉我,我缺少的是什么?请参阅下面的code:
服务:
cmApp.service('supplierService',函数($ HTTP,$ Q){
this.getSuppliers =功能(orderByColumn,skipRows,takeRows){
变种推迟= $ q.defer();
$ HTTP({
方法:GET,
网址:'API /供应商,
PARAMS:{排序依据:orderByColumn,跳过:skipRows,取:takeRows},
超时:30000,
缓存:假的
})。
成功(功能(数据,状态,头,配置){
//此处所需的任何额外的处理
deferred.resolve(数据,状态,头,配置);
})。
错误(功能(数据,状态){
deferred.reject(数据,状态,头,配置);
});
返回deferred.promise;
}
控制器:
supplierService.getSuppliers($ scope.orderby,$ scope.skip,$ scope.take)
。然后(功能(数据,状态,头,配置){
** //这里得到不确定的。**
$ scope.totalRecords = parseInt函数(标题(X-TotalRowCount'));
$ scope.suppliers =数据;
},功能(错误){
//的错误处理
});
解决方案
我发现我自己的解决方案。我所要做的就是创建一个数组,并添加所有这些值相同&放大器;其返回到控制器。请参阅更新code以下:
服务:
cmApp.service('supplierService',函数($ HTTP,$ Q){
this.getSuppliers =功能(orderByColumn,skipRows,takeRows){
变种推迟= $ q.defer();
$ HTTP({
方法:GET,
网址:'API /供应商,
PARAMS:{排序依据:orderByColumn,跳过:skipRows,取:takeRows},
超时:30000,
缓存:假的
})。
成功(功能(数据,状态,头,配置){
//此处所需的任何额外的处理
变种结果= [];
results.data =数据;
results.headers =报头();
results.status =状态;
results.config =配置; deferred.resolve(结果);
})。
错误(功能(数据,状态){
deferred.reject(数据,状态,头,配置);
});
返回deferred.promise;
}
控制器:
supplierService.getSuppliers($ scope.orderby,$ scope.skip,$ scope.take)
。然后(功能(响应){
$ scope.suppliers = response.data;
$ scope.totalRecords = parseInt函数(response.headers [X-totalrowcount]);
},功能(错误){
//的错误处理
});
I am trying to access the http headers in my angular controller but I am getting undefined. Also, I am able to see the header response in my angular service which is not reflecting in my controller. Can someone please tell me what I am missing? Please see the code below:
Service:
cmApp.service('supplierService', function ($http, $q) {
this.getSuppliers = function (orderByColumn, skipRows, takeRows) {
var deferred = $q.defer();
$http({
method: 'GET',
url: 'api/supplier',
params: { orderBy: orderByColumn, skip: skipRows, take: takeRows },
timeout: 30000,
cache: false
}).
success(function (data, status, headers, config) {
// any required additional processing here
deferred.resolve(data, status, headers, config);
}).
error(function (data, status) {
deferred.reject(data, status, headers, config);
});
return deferred.promise;
}
Controller:
supplierService.getSuppliers($scope.orderby, $scope.skip, $scope.take)
.then(function (data, status, headers, config) {
**//getting undefined here.**
$scope.totalRecords = parseInt(headers('X-TotalRowCount'));
$scope.suppliers = data;
}, function (error) {
// error handling here
});
解决方案
I have found the solution by myself. All I have to do is create an array and add all those values to the same & return it to the controller. Please see the updated code below:
Service:
cmApp.service('supplierService', function ($http, $q) {
this.getSuppliers = function (orderByColumn, skipRows, takeRows) {
var deferred = $q.defer();
$http({
method: 'GET',
url: 'api/supplier',
params: { orderBy: orderByColumn, skip: skipRows, take: takeRows },
timeout: 30000,
cache: false
}).
success(function (data, status, headers, config) {
// any required additional processing here
var results = [];
results.data = data;
results.headers = headers();
results.status = status;
results.config = config;
deferred.resolve(results);
}).
error(function (data, status) {
deferred.reject(data, status, headers, config);
});
return deferred.promise;
}
Controller:
supplierService.getSuppliers($scope.orderby, $scope.skip, $scope.take)
.then(function (response) {
$scope.suppliers = response.data;
$scope.totalRecords = parseInt(response.headers["x-totalrowcount"]);
}, function (error) {
// error handling here
});
这篇关于AngularJS - 访问HTTP头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文