Thursday, October 21, 2021

How to fetch data from server using XHR method

const xhttp = new XMLHttpRequest()

xhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    const data = JSON.parse(this.responseText)
    // handle the response that is saved in variable data
  }
}

xhttp.open('GET', '/data.json', true)
xhttp.send()

No comments:

Post a Comment

JavaScript Functions as JavaScript Variables

In Javascript instead of declaring and executing a function in two different steps. for example step 1 -  function add(a,b){return a+b;} ste...