DEVELOPER SANDBOX Academy of Mastery
WelcomeBuild · Explore · Create SD
LAB / EXPLORATION

Rebuilding Fetch with XMLHttpRequest

Complete

Builds a simplified data request from scratch with XMLHttpRequest

Project summary

This learning project reconstructs the basic request flow behind modern JavaScript data fetching. It creates an XMLHttpRequest, opens and sends a GET request, observes readyState, receives raw JSON as responseText, parses it with JSON.parse(), and accesses the resulting JavaScript objects and arrays. The goal is to understand the mechanics underneath higher-level APIs such as fetch() before returning to the Modern Agenda ECMAScript implementation.

Technologies

ECMAScript
JavaScript
XMLHttpRequest
JSON
HTTP

Code


  function myFetch(path){
    const request = new XMLHttpRequest();
    // console.log(request);
     request.open("GET", path);
     request.onreadystatechange = function() { 
        if (this.readyState === 4){
            // console.log(this.responseText);
            // console.log(typeof this.responseText);
            const data = JSON.parse(this.responseText);
            console.log(data[0].sections);
        }
       
    };
    request.send();
    }
myFetch("../data/agenda-data.json");

Concepts:

Request lifecycle
GET requests
response objects
readyState
event handlers
JSON parsing
nested data access