Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Developer and personalized recommendations.Start Your Free Trial Now
- View Offline
- What is AJAX?
- Making requests asynchronous
- Updating the DOM with getElementById
- Reading JSON files
- Working with jQuery and AJAX
- Sending and searching JSON data
- Incorporating CSS transitions
Skill Level Intermediate
I wanted to take a minute to look at this and highlight some of the events that the request triggered. So I'm going to switch over to my browser, and I'm going to load up this page, and then I'm going to right- click and select Inspect Element. Then I'm going to switch over to the console and take a look at this object that's returned from my server. So I'll open up that up, and you'll see that there's a lot of different events right here. They all start with on. you see onabort, onerror, onload. There's an event that we're interested in called onreadystatechange. The browser's AJAX API maintains a property called readyState that has a number indicating how far along the request has progressed.
If the Value is 0, for example, then the request hasn't been sent yet. Once the value of that property reaches 4, then the operation of sending and receiving the requests has been completed. So what we want to do is modify our code so that it checks the status of this property. If readyState has the value of 4, then our asynchronous request has been received and we can do something with the data we get back. First, before we send our request, we want to change our open method and take out the false parameter at the end. We could send a Boolean value of true, but this is the default and so we don't really need to pass it.
Now we can insert an event handler. Event handlers will run a function when something happens. We want to know when the onreadystatechange event changes. So before we send the request, we'll check that event, and this will be a function literal, so I'll take out the name. And then I'm going to grab all this right here and paste it inside this event handler. I also want to modify our if statement so that it not only checks for the request status, but also for the readyState value.
So I'll copy this and I'll paste it twice, and then I'll change this one to say request.readyState. So this should check to see if the value of request.readyState is equal to 4, and also if the request.status is 200. So let me go ahead and save that, and I'll switch over to my browser, and I'll reload the page. And you see that we get the same HTML and we get the same result, but the way we got the result is a little bit different.
So, although it doesn't look any different, this is an honest-to-goodness proper AJAX request. It sends a request to our server and then waits for that request to come back at some point; that's called asynchronously. If you want to learn more about the XHR requests, take a look at this document from Mozilla. It's a good resource to see all of the different options available in XHR requests.