Angular 4 single API request running multiple times

倾然丶 夕夏残阳落幕 提交于 2019-11-28 10:19:53

问题


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)});


回答1:


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.




回答2:


This is a default browser feature. every time when you run a HTTP request your browser will run that twice one with a method you specified and other is for method OPTION where browser checks either that request is exist or not.



来源:https://stackoverflow.com/questions/48224733/angular-4-single-api-request-running-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!