New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Making requests asynchronous

From: JavaScript and AJAX

Video: Making requests asynchronous

So AJAX requests are supposed to be asynchronous by nature, which means that they should run in the background independently of other events; otherwise they would be called synchronous requests and the language would probably be called SJAX, right? Really, it's the whole reason AJAX is awesome, so let's take a look at making asynchronous requests. So I have a project here open with three files: a data file with the words Hello World, an HTML file whose sole purpose is to call a JavaScript document, as well as this JavaScript document, with a single synchronous request.

Making requests asynchronous

So AJAX requests are supposed to be asynchronous by nature, which means that they should run in the background independently of other events; otherwise they would be called synchronous requests and the language would probably be called SJAX, right? Really, it's the whole reason AJAX is awesome, so let's take a look at making asynchronous requests. So I have a project here open with three files: a data file with the words Hello World, an HTML file whose sole purpose is to call a JavaScript document, as well as this JavaScript document, with a single synchronous request.

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.

Show transcript

This video is part of

Image for JavaScript and AJAX
JavaScript and AJAX

21 video lessons · 18494 viewers

Ray Villalobos
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.