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 trial

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

Arsalan Raja
Arsalan Raja
6,508 Points

Question about variable var xhr = new XMLHttpRequest();

in this video in javascript code Dave create new instance and stored in variable xhr. I didnt understand why did he create new instance and as i know you can create instance from constructor function and there is no constructor function.

1 Answer

All modern browsers have a "built-in" function called XMLHttpRequest(). Since its built-in you don't have to define it anywhere. Its an object constructor and you use it to create an object that can be used to exchange data with a server behind the scenes. (Since AJAX isn't simply JavaScript running in your browser).

Maria Campbell
Maria Campbell
8,641 Points

That is not true. I followed Dave exactly in the work spaces, and nothing happened. I got an error in the JS Console that stated:

Uncaught ReferenceError: XMLHTTPRequest is not defined

referring to the code:

var xhr = new XMLHTTPRequest();

Now why is THAT happening?