From the course: Learning App Building with Vanilla JavaScript

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Create an Ajax request with Fetch

Create an Ajax request with Fetch - JavaScript Tutorial

From the course: Learning App Building with Vanilla JavaScript

Start my 1-month free trial

Create an Ajax request with Fetch

- [Instructor] So, we've opened script.js from the js folder in the start files for this video and this contains the code for this app. And so if we scroll down, it's down here in lines 23 to 27 that contain the code for the Ajax request that fetches the weather data for whatever location the user entered. This code uses the jQuery get method which uses syntax similar to promises and fetch in vanilla JavaScript. So, after the get request, the done method is chained capturing the response and then calling updateUISuccess which is a function and passing along that response object. Now, the done method fires only if the request is successful. And then after the done method, the fail method is chained and this is called only if the Ajax request is unsuccessful. And for our app, the fail method simply calls the updateUIFailure function. Now, because the jQuery structure is so similar to fetch, rewriting this code as vanilla JavaScript fetch request is pretty easy. So, I'm going to start by…

Contents