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!

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 trial

JavaScript AJAX Basics (retiring) AJAX Concepts A Simple AJAX Example

New XMLHttpRequest for each AJAX request, why?


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.


Neil McPartlin
Neil McPartlin
14,662 Points

I'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...

2:08 In fact, for each ajax request, you should create a new XHR object.

2:13 For example, if you wanted to use ajax twice on a page, to

2:16 request data for a sidebar, and another to process a form submission for example.

2:21 You will need two variables, each with their own XHR object.

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.

1 Answer


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
Neil McPartlin
14,662 Points

I'm glad your found the answer you required :)