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.

Ryan Mayo
Ryan Mayo
12,631 Points

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

Matthew Fung
Matthew Fung
5,797 Points

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.