My request for data from server using the XMLHttpRequest, keeps returning undefined

I tried to request data from this server address https://codexpression.github.io/catz/catz.json using AJAX XMLHttpRequest, but when I tried to console.log the property value of the text, I keep getting “undefined”, please find code below

function getData() {
  let requestObject = new XMLHttpRequest();            //create XMLHttpRequest Object

  requestObject.onreadystatechange = function() {      //create an onstatechange function
    if (this.readyState == 4 && this.status == 200) {  //check if request is successful
      // myFunction(this)
      let convert = JSON.parse(this.responseText)
      console.log(convert.all.text)
    }
  };

  requestObject.open("GET", "https://codexpression.github.io/catz/catz.json", true); //get the document address and send request
  requestObject.send()
}

getData()

I am not in front of my computer to check, but what does just this.responseText print? Does it display a large blob of data?

I think it worked
it returns the below

  1. {results: Array(1), info: {…}}

  2. info: {seed: “0d23199c55f504d6”, results: 1, page: 1, version: “1.3”}

  3. results: Array(1)

1. 0: {gender: "female", name: {…}, location: {…}, email: "[email protected]", login: {…}, …}
2. length: 1
3. __proto__: Array(0)
  1. proto: Object

It did seem to return an array of data! :slight_smile:

1 Like

thank you, so I need to use array methods i.e index notation instead of the dot notation, thanks it worked

1 Like

It all depends on how the data gets returned. You’ll have to inspect the raw data returned and figure out the right next step from there :partying_face:

Oftentimes, the API documentation can be helpful as well.