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.
Handle XHR success and failure - JavaScript Tutorial
From the course: JavaScript: Ajax and Fetch
Handle XHR success and failure
- [Instructor] My code, so far, creates and sends a request and then logs whatever's in the response to the console. Now, eventually, I want to be working with the response data to change what users see in the browser window. But, until I get there, I at least want to update my code so it differentiates between a successful response and a response containing an error message. And I'll break those out into separate functions to keep my concerns separate so the AJAX request code can be changed without affecting the code to work with the response. So first, I'm going to break out my code for analyzing the response into a new function and I'll call that response method. And that's going to take the HTTP request parameter. And so, within that function, I want to check a couple properties of that request. Now, the ready state property will tell us if the request is completed. That's the case when the value's four. So, I will check if HTTP request .ready state, triple equals four, and then…
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.
Contents
-
-
-
What is Ajax?2m 23s
-
(Locked)
Understand asynchronous program flow2m 53s
-
(Locked)
Get an API key6m 42s
-
(Locked)
Research and create an Ajax request URL3m 50s
-
(Locked)
Create an XHR request5m 13s
-
(Locked)
Test an XHR request5m 51s
-
(Locked)
Handle XHR success and failure5m 38s
-
(Locked)
Challenge: Build an XHR request56s
-
(Locked)
Solution: Build an XHR request5m 3s
-
-
-
-
-
-