这个简单的问题困扰着我.我在Web Api Rest服务器的响应头中有一个自定义值.我可以把它看作Firebug:X-TotalPages 204我试着在我的AngularJS控制器中得到它.代码如下.但我找不到任何好的例子如何做到这一点.的console.log(头()[ 'X-总页数']); 记录'undefined'
var assets = angular.module("Assets", ['ui.bootstrap']);
assets.controller('AssetSearchController', function ($scope, $http) {
$scope.test = 'very';
$scope.getItems = function () {
$http({ method: 'GET', url: 'http://localhost:57772/api/assets', params: { search: 1, page: 0, pageSize: 10 } }
).success(function (data, status, headers, config) {
$scope.currentPage = 4;
console.log(headers()['X-TotalPages']);
$scope.items = data;
}).
error(function (data, status) {
console.log(JSON.stringify(data));
console.log(JSON.stringify(status));
});
};
Run Code Online (Sandbox Code Playgroud) 我试图访问我的角度控制器中的http标头,但我得到了未定义.此外,我能够看到我的角度服务中的标题响应,这在我的控制器中没有反映出来.有人可以告诉我我错过了什么吗?请参阅以下代码:
服务:
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;
}
Run Code Online (Sandbox Code Playgroud)
控制器:
supplierService.getSuppliers($scope.orderby, $scope.skip, $scope.take)
.then(function (data, status, headers, config) {
**//getting undefined here.**
$scope.totalRecords …Run Code Online (Sandbox Code Playgroud) Authentication:76efbc0946773b62c93e952b502a47acd898200f6f80dc46ac87ffc501c00780当我用检查器检查请求时,我的http响应包含头部身份验证(如此处所述:),但是调用headers("Authentication")返回null
return $http({
method: "GET",
url: url,
headers: {
'Content-Type': "application/json"
}
}).success(function (data, status, headers, config) {
console.log(headers("Authentication"));
})
Run Code Online (Sandbox Code Playgroud)
你对我的做法有什么看法吗?
仅供参考,我已经尝试将其切换回"承诺"方式,.then问题仍然存在.
POST的回调函数为我的自定义HTTP标头返回null X-Auth-Token.Chrome正在显示正确的POST响应标头,但Angular.js不是.
Angular唯一返回的是Cache-Control和Content-Type.其他一切都显示为null.
这是我的CoffeeScript显示我是如何调用它的:
.factory 'loginFactory', ($rootScope, $http, $resource) ->
$resource '/api/auth/login',
email: '@id'
password: '@id'
.controller 'userController', ($scope, $state, $http, loginFactory, userService) ->
$scope.validationError = false
$scope.user =
email: ''
password: ''
$scope.loginUser = ->
loginFactory.save $scope.user, (u, headers) ->
console.log headers('X-Auth-Token')
.$promise.then (response) ->
unless response.error
userService.login($scope.user.email, $scope.user.password)
unless userService.redirIfLoggedIn()
$scope.validationError = true
Run Code Online (Sandbox Code Playgroud)
我也试过运行早期版本的Angular 1.3.x,那些也有同样的问题.
为什么Angular只在我提出请求时返回这两个标题?
我正在资源上实现自定义响应标头,在阅读了类似的问题后,我在 CORS 标头中添加了一个“Access-Control-Expose-Headers”,但我不能正确配置某些内容。
以下是我在资源的 GET 请求中收到的标头:我收到“Steve”的“Response-Header”,“Access-Control-Expose-Headers”应该允许
Access-Control-Allow-Headers:x-requested-with, Request-Header, Response-Header
Access-Control-Allow-Methods:POST, GET, OPTIONS, DELETE
Access-Control-Allow-Origin:*
Access-Control-Expose-Headers:Response-Header
Response-Header:Steve
Run Code Online (Sandbox Code Playgroud)
但是,Angular 仍然无法访问该自定义标头。我找回了我的自定义请求头,但不是我的自定义响应头:
{
"data": {
"id": 2,
"content": "Hello, frank!"
},
"status": 200,
"config": {
"method": "GET",
"transformRequest": [
null
],
"transformResponse": [
null
],
"url": "http:\/\/localhost:8080\/greeting",
"headers": {
"Accept": "application\/json, text\/plain, *\/*",
"Request-Header": "frank"
}
},
"statusText": "OK"
}
Run Code Online (Sandbox Code Playgroud)
Angular 1.3 $httpProvider.interceptor:
'use strict';
angular
.module('headerDemoUiApp')
.factory('AuthInterceptor', function AuthInterceptor(nameService) {
return {
request: handleRequest,
response: handleResponse
};
function handleRequest(config) {
if …Run Code Online (Sandbox Code Playgroud) 我使用Apiary.io模拟我的API.但不知怎的,我无法使用angularJS从响应对象中读取任何标头.而且我确信我至少通过检查firebug来正确设置Content-Type:application/json.Angular中的代码也应正确读取标题,因为我可以在向apiary.io发送请求时将其打印出来...
$http.get('http://ies.apiary.io/some').then(function(response) {
console.log("ok",response.headers('Content-Type'));
},function(response){console.log("err",response);});
Run Code Online (Sandbox Code Playgroud)
我有一个GO语言的REST API和Angularjs中的前端,但是当我在angular中获取我的资源时,我的自定义头文件不存在.
控制器:
Persons.query(
function (data, headerGetter, status) {
var headers = headerGetter();
console.log(headers["X-Total-Count"]); //PRINT: undefined
console.log(headers) //PRINT: {Content-Type:application/json;charset=utf-8}
console.log(data); //PRINT: [{name:'mr x', age:'67'}, ....]
},
function (error) {
console.error(error);
});
Run Code Online (Sandbox Code Playgroud)
模型:
myApp.factory("Persons", function ($resource) {
return $resource(api_url+"/persons");
});
Run Code Online (Sandbox Code Playgroud)
响应Chrome或Firefox,任何客户:
Access-Control-Allow-Methods:GET
Access-Control-Allow-Origin:*
Content-Length:1839
Content-Type:application/json; charset=utf-8
Date:Thu, 12 Mar 2015 21:53:54 GMT
X-Total-Count:150
Run Code Online (Sandbox Code Playgroud) angularjs ×7
http-headers ×2
javascript ×2
angular-http ×1
apiary.io ×1
cors ×1
rest ×1
spring ×1