This simple problem is bugging me. I have a custom value in my response header from the Web Api Rest server. I can see it it Firebug as: X-TotalPages 204 I try to get it in my AngularJS controller. Code below. But I cant find any good examples how to do this. console.log(headers()['X-TotalPages']); logs '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)); }); };
The $http service is a core AngularJS service that facilitates communication with the remote HTTP servers via the browser's XMLHttpRequest object or via JSONP. For unit testing applications that use $http service, see $httpBackend mock.
Use the HttpClient.get() method to fetch data from a server. The asynchronous method sends an HTTP request, and returns an Observable that emits the requested data when the response is received. The return type varies based on the observe and responseType values that you pass to the call.
The Access-Control-Expose-Headers response header allows a server to indicate which response headers should be made available to scripts running in the browser, in response to a cross-origin request. Only the CORS-safelisted response headers are exposed by default.
Answer: C is the correct option. The ng-bind directive is used to bind the application data to the HTML view in the AngularJS application.
You should use: headers('X-TotalPages')
(Posting Wawy's answer so the question can be resolved.)
For $http(url).then() syntax which replaced $http(url).success().error() in newer versions of AngularJS, I used this:
$http(url).then(function(response){ response.headers("X-TotalPages"); });
Just using response.headers() will give all headers attached in response.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With