Start learning with our library of video tutorials taught by experts. Get started

JavaScript and AJAX

Making requests asynchronous


From:

JavaScript and AJAX

with Ray Villalobos

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.

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
JavaScript and AJAX
1h 12m Intermediate Jan 18, 2013

Viewers: in countries Watching now:

The ability to read data and communicate information asynchronously is fundamental to modern web applications. In this course, author Ray Villalobos takes a look at integrating AJAX (Asynchronous JavaScript and XML) with JavaScript and jQuery—a combination that makes it easy to work with dynamic data and reduces the number of requests to the server. The course first explains what AJAX is, and shows how to load and reload pages with pure JavaScript, update the DOM, and read and parse data in the XML and JSON formats. Next, learn how to read and write information between the server and the client with AJAX methods in jQuery, and build a dynamic search application that updates search results as you type.

Topics include:
  • 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
Subjects:
Developer Web Programming Languages Web Development
Software:
Ajax JavaScript
Author:
Ray Villalobos

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.

There are currently no FAQs about JavaScript and AJAX.

Share a link to this course
Please wait... Please wait...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed JavaScript and AJAX.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

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