Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialpewpew
9,792 PointsNew XMLHttpRequest for each AJAX request, why?
Hi,
Why is it recommended/required in this video that I should create a new XMLHttpRequest
for each AJAX request I make?
I created several functions to call different API endpoints with the same XMLHttpRequest
object and it worked fine so far.
Thanks!
1 Answer
pewpew
9,792 PointsHi,
I found the answer I was looking for here, that's the important part:
Note: Calling this method for an already active request (one for which open() or openRequest() has already been called) is the equivalent of calling abort().
Neil McPartlin
14,662 PointsI'm glad your found the answer you required :)
Neil McPartlin
14,662 PointsNeil McPartlin
14,662 PointsI'm new to this topic, I've just watched the video. You may need to edit your question to say... 'New XMLHttpRequest 'object' for each AJAX request, why?'
I know you are referring to this video comment by Dave...
So in the video, Dave created a single object which he called 'xhr' and it is associated with the single destination 'sidebar.html'. This is the only destination with which 'xhr' is associated. You mention being able to call different API endpoints, but it would be my understanding that you could not re-use the 'xhr' object, but instead you would need to create a new one for each additional endpoint.