简体   繁体   中英

Beginner Js : REST API returns "undefined" on successful call

    {
  let request = new XMLHttpRequest();
  request.open('GET', url);
  request.responseType = "text";
var response = XMLHttpRequest.responseText;
  request.send();
  document.getElementById("price").innerHTML = response;
}

What could be my mistake? I am trying to print the response as text but it simply says "undefined"

You have to listen for response data by onreadystatechange

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
       document.getElementById("price").innerHTML = xhttp.responseText;
    }
};
xhttp.open("GET", url, true);
xhttp.send();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM