From the course: JavaScript: Ajax and Fetch
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Create a fetch request - JavaScript Tutorial
From the course: JavaScript: Ajax and Fetch
Create a fetch request
- [Instructor] My form page for Explore California currently uses an XHR request to get remote data. I want to update that to fetch to make it easier to read and to maintain. To do this, I need to replace two functions, createRequest creates an XHR object and responseMethod works with the properties of the XHR object. I'll start by commenting these out. So there's responseMethod, and there's createRequest. And next, I want to recreate the createRequest function using fetch. So that'll be const createRequest = function takes url, succeed and fail as parameters, got to spell that right, C-C-E-E-D, and then I'm going to call fetch, pass it my URL, which is a parameter, and because I'm going to be chaining then to this, I'm going to do that on a separate line. So this is pretty common so that it's clear that .then is chained to the fetch that's above it. And then I need to pass in a function, and I'm going to use arrow function syntax. So, the parameter for this function is (response) and…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.