Search code examples
angulartypescriptangular4-httpclient

Angular 4 single API request running multiple times


I am having issue regarding Angular 4 App. I have created an app in which data is getting from third party APIs. The issue I am getting is my APIs are calling for multiple times but I sent request only once.

After some R&D I found browser is running flight request with OPTION method but after checking all APIs I found they are running for multiple times except flight request. Like fir an API I am requesting API once and it is running for 4 times 2 times for option and 2 times for GET request.I check my code and only one time request is running.

I totally understand and agree with flight request.

I analyzed all the calls on my local and here is the finding: 1. all GET calls have corresponding OPTIONS call, which as per some artcle is not a problem 2. However, there are multiple calls for these:

http://localhost:3005/tasks?taskID=1713&singular=1 = 4 Get call, 2 OPTIONS call  - why?
http://localhost:3003/resources/30/configs = 2 GET call, 1 OPTIONS call - why?
http://localhost:3003/resources/146/configs = 2 GET cal, 1 OPTIONS call - why?
http://localhost:3003/resources/30/settings = 2 GET call, 1 OPTIONS call - why?
http://localhost:3003/resources/146/settings = 2 GET call, 1 OPTIONS call - why?

These are the only calls where GET is multiple times. For all other calls, /environments, /settings, /admin,/currentevent,/currentshift.. get call is going only once.

return this.http.get(this.environment.SFDCServiceHost + 'getCurrentEvent?resourceId=' + id + '&singular=1', {headers: this._helperService.setRequestHeader(id)});

Browser requests


Solution

  • There May be some issue with your code. Browser doesn't add run any extra request except flight request. To check this you first check how many time that function is calling. I am very sure about this because I had the same issue.