Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS $http response header

Tags:

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));         });      }; 
like image 856
Jens Alenius Avatar asked Feb 27 '14 09:02

Jens Alenius


People also ask

What is $HTTP in AngularJS?

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.

How do we pass data and get data using http in angular?

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.

What is Access Control expose headers?

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.

Which of these option is correct for setting header in AngularJS http POST request?

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.


2 Answers

You should use: headers('X-TotalPages')

(Posting Wawy's answer so the question can be resolved.)

like image 194
barsju Avatar answered Oct 26 '22 02:10

barsju


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.

like image 29
Eshank Avatar answered Oct 26 '22 01:10

Eshank