0

I am using angularjs 1.x with my project and passing custom headers from $http.get call. But unfortunately this header is not accessible on server side. I have googled and found so many example to pass but all are not working.

my angularjs code.

this.GetAll = function () {
            ̶v̶a̶r̶ ̶d̶e̶f̶f̶e̶r̶e̶d̶ ̶=̶ ̶$̶q̶.̶d̶e̶f̶e̶r̶(̶)̶;̶
            var config = {
                headers: {
                    'API_KEY': 'abc'
                }
            };
            var query = $http.get(appSetting.apiBaseUrl + "api/Blogs/GetHomeArticles", config);

            return query.then(function (response) {
                console.log("response.data", response.data);
                ̶d̶e̶f̶f̶e̶r̶e̶d̶.̶r̶e̶s̶o̶l̶v̶e̶(̶r̶e̶s̶p̶o̶n̶s̶e̶.̶d̶a̶t̶a̶)̶;̶
                return ̶d̶e̶f̶f̶e̶r̶e̶d̶.̶p̶r̶o̶m̶i̶s̶e̶;̶ response.data;
            }, function (response) {
                console.log("response.error", response);
                ̶d̶e̶f̶f̶e̶r̶e̶d̶.̶r̶e̶j̶e̶c̶t̶(̶r̶e̶s̶p̶o̶n̶s̶e̶)̶;̶
                ̶r̶e̶t̶u̶r̶n̶ ̶d̶e̶f̶f̶e̶r̶e̶d̶.̶p̶r̶o̶m̶i̶s̶e̶;̶
                throw response;
            });
};

Kindly help on this.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Mukesh Kumar
  • 2,354
  • 4
  • 26
  • 37
  • 1
    Where do you declare "deffered"? – Marcus Höglund Aug 17 '17 at 18:37
  • What does the browser show is being transferred to the server (*hint: use the browser's debugging window/tools*)? Is your header missing there as well? If not its a problem with your .net code (not shown), otherwise there is something not right about your config. – Igor Aug 17 '17 at 18:49
  • Yes... it is being transferred to web api code and debugging is going on. But HttpRequestMessage header does not contain my header. – Mukesh Kumar Aug 17 '17 at 18:52
  • So why share the javascript code if the problem is likely in your web api code? You should share/post the web api code... – Igor Aug 17 '17 at 18:55
  • Likely a [CORS issue](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS). Be sure that the pre-flight response includes a proper [Access-Control-Allow-Headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers) header. – georgeawg Aug 18 '17 at 00:38
  • Though not the source of your problem, avoid using a [deferred anti-pattern](https://stackoverflow.com/questions/30750207/is-this-a-deferred-antipattern). – georgeawg Aug 18 '17 at 00:46

0 Answers0