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) Programming AJAX Stage 2 Challenge

Ram Iyer
Ram Iyer
9,116 Points

What triggers the xhr.send() ?

Since we have written the method directly inside the js file, does it load everytime the file loads. If it does, then how is this useful.

Shouldnt there be a button or something like 'check current status' on which the boss can click and then the AJAX request is sent.

It depends on the scope of the project. If this is a page that is either refreshed regularly one way or another, then the given script would probably be fine.
If not then, yes, a button or something more exotic like a timer that triggers the method could be appropriate. It's something that you could add pretty easily (especially as he had given an example earlier in this course.)

I assume that it wasn't covered here because it had already been covered.

1 Answer

The AJAX request is sent on file load, as the browser reads the file. You could, certainly add some kind of timer or button event to trigger it.

The reason that it isn't in this instance is that this course is intended as an introduction. Adding more code to open a AJAX request is unnecessary, since you should be able to change the behaviour in your own projects.