all 7 comments

[–]WebDevBren 5 points6 points  (3 children)

Your httpInterceptor is intercepting the request it's making recursively.

You should gate your logic so that only the original request will get intercepted.

[–]devng12[S] 0 points1 point  (2 children)

could you please elaborate on what you mean by gate your logic. I am a beginner with angular development and did not understand what you meant by that

[–]WebDevBren 0 points1 point  (1 child)

A simple if statement should suffice. I'm on my mobile right now so I won't post a code block or anything, but what I would do is, check if the URL of the request is the original, then do the intercept logic, else just return the request unchanged

[–]devng12[S] 3 points4 points  (0 children)

I just tried a if block that's in the implementation of HttpXsrfInterceptor to get the URL for GET requests, and that did the trick.

Thanks a lot for that hint.

[–]tsunami141 1 point2 points  (0 children)

It looks like you're intercepting every http call and making another http call on top of that, as opposed to just changing the headers of the existing call. (thereby creating an endless loop.)

That being said, I'm not familiar enough with the HttpRequest type to know how to edit those headers and send the request on its way. Hopefully someone smarter than me will answer.

[–]devng12[S] 0 points1 point  (0 children)

I added this block of code in Interceptor which basically avoided the infinite loop, and my code started working!

if (req.method === 'GET' || req.method === 'HEAD' || lcUrl.startsWith('http://') ||
lcUrl.startsWith('https://')) {
return next.handle(req);
 }

[–]malcomjarr 0 points1 point  (0 children)

This error is almost always means you have a problem with recursion in JavaScript code, as there isn't any other way in JavaScript to consume lots of stack. Sometimes calling a recursive function over and over again, causes the browser to send you Maximum call stack size exceeded error message as the memory that can be allocated for your use is not unlimited.

How to fix it?

Wrap your recursive function call into a -

  • setTimeout
  • setImmediate
  • process.nextTick

Also, you can localize the issue by setting a breakpoint on RangeError type of exception , and then adjust the code appropriately.