I have a REST API in GO language and the front-end in Angularjs , but when I get my resource in angular my custom header don't exist.
Controller:
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);
});
Model:
myApp.factory("Persons", function ($resource) {
return $resource(api_url+"/persons");
});
Response Chrome or Firefox, any client:
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
So this IS a CORS issue. Setting
in the server response solves your problem.
You will need to parse the response to a number:
You're making a request from a different domain than the one where your
API
is located, operation which is calledCross-site HTTP requests ( CORS )
In order to use custom headers you need to set another one called
Access-Control-Expose-Headers
The way I do it in
.NET
( I suppose it's kinda similar inGo
):And in
AngularJS
I'm getting headers like this :