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

Handle XHR success and failure - JavaScript Tutorial

From the course: JavaScript: Ajax and Fetch

Start my 1-month free trial

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…

Contents