Skip to content
Advertisement

Javascript for loop only giving one output

I’ve been toying with this for far too long…

JavaScript

I have no clue why that for loop at the end only sends one GET request. Please, spare me my sanity… I just don’t get it. The array “urls” contains the information I need, and the variable “sendstr” works perfectly well… Why then does my terminal only show that the first result is being given?

Advertisement

Answer

Each XMLHttpRequest can only send one request. As stated in MDN, calling open on an already open request is equivalent to aborting the request. So, create a new XMLHttpRequest for each loop:

JavaScript

Alternatively, migrate to fetch:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement