Answers for "vanilla javascript api request"

0

vanilla javascript api request

var xhr = new XMLHttpRequest();

xhr.onload = function () {

	if (xhr.status >= 200 && xhr.status < 300) {

      const users = JSON.parse(xhr.response);
      const content = document.getElementById("user_content");
    
      for(let i in users){
          let user_name = document.createElement("p");
          let text = document.createTextNode(`User ID ${users[i].id}: ${users[i].title}`);
          user_name.appendChild(text);
          content.appendChild(user_name);
      }
    
	} else {
      console.log('The request failed!');
	}

	console.log('This always runs...');
};

xhr.open('GET', 'https://jsonplaceholder.typicode.com/posts');
xhr.send();
Posted by: Guest on March-10-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language