My server returns this kind of header: Content-Range:0-10/0
:
I tried to read this header in angular with no luck:
var promise = $http.get(url, { params: query }).then(function(response) { console.log(response.headers()); return response.data; });
which just prints
Object {content-type: "application/json; charset=utf-8"}
Any ideas how to access the content range header?
You can obtain the full response using the observe property and specifying the response value. This way Angular will hand you the full HttpResponse object. Let's see this with an example. We simply import HttpClient from the @angular/common/http package and inject is as httpClient via the service constructor.
$http is an AngularJS service for reading data from remote servers.
Observe ResponseHttpClient object allows accessing complete response, including headers. In the browser, response body is a JSON object, which can be copied to a typescript interface or class type. Response headers are key/value pairs.
The $scope in an AngularJS is a built-in object, which contains application data and methods. You can create properties to a $scope object inside a controller function and assign a value or function to it. The $scope is glue between a controller and view (HTML).
Use the headers
variable in success and error callbacks
From documentation.
$http.get('/someUrl'). success(function(data, status, headers, config) { // this callback will be called asynchronously // when the response is available }) .error(function(data, status, headers, config) { // called asynchronously if an error occurs // or server returns response with an error status. });
If you are on the same domain, you should be able to retrieve the response headers back. If cross-domain, you will need to add Access-Control-Expose-Headers
header on the server.
Access-Control-Expose-Headers: content-type, cache, ...
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