Wednesday, June 13, 2018

JavaScript - use XMLHttpRequest

Below code demonstrates usage of  XMLHttpRequest:


var http = new XMLHttpRequest();
var url = 'http://jsonplaceholder.typicode.com/posts';
var method = 'GET';



http.open(method,url);
http.onreadystatechange = function() {
 if(http.readyState === XMLHttpRequest.DONE && http.status === 200) {
    myJSON = JSON.parse(http.responseText)
    console.log(myJSON[0].id);
 } else if (http.readyState === XMLHttpRequest.DONE && http.status !== 200) {
    console.log("Error!");
 }
};
  
http.send();


Refer:

Practice at:

No comments:

Post a Comment