Its an old issue but posting my thought for future readers. Will try to keep it short.
THEORY
To me the issue looks to be related to CORS (Cross Origin Resource Sharing)
. When www.domain1.com
makes a call to www.domain2.com
, it is considered unsafe and browser needs some special headers to process your request (read Access-Control-Allow-Origin
, Access-Control-Allow-Methods
). Even www.domain:1000.com
and www.domain:2000.com
are considered separate domains.
Why POSTMAN works?
POSTMAN (even Telerik Fiddler) is a tool and not browser. Hence they does not care about any headers in response and renders the response. That's why they works. But when you run it via Angular, the app opens in browser and hence you see the issue of CORS playing the spoilsport.
SOLUTION
In the end, its an issue from server side code and not client side. Your Angular code is fine, you need to do either of the following to fix it.
- Permanent: Enable CORS in server side code. Read about it on internet more.
- Temporary: Install Chrome Extension for CORS (many free available) and enable the status of the plugin. Your Angular app should start working just fine.
Side Note
Regarding your code to add new request headers, should not it be part of some HTTP Interceptor?
I understand that you just wanted to present the issue and it may not be your original code. But still wanted to point this out.
Hope it helps.