JSFiddle - React, Tailwind, and code Playground
by noblood
JavaScript
// XMLHttpRequest review //
var xhr = new XMLHttpRequest();
var url = "yoururl"
xhr.open("GET", url);
xhr.setRequestHeader("User-Agent", "XMLHttpRequest");
xhr.send()
// Check status
if(xhr.status != 200) {
alert("Error");
}
// Determine the type of data in the response
function getResponse(xhr) {
var type = xhr.getResponseHeader();
switch(type) {
case "text/xml":
return xhr.responseXML;
case "text/json":
return JSON.parse(xhr.responseText);
default:
return xhr.responseText;
}
}
// Handling an ansynchronous response onreadystatechange
xhr.onreadystatechange = function() {
if(xhr.readyState === 4) {
var response = JSON.parse(xhr.reponseText);
}
}
// Transmitting data with a request
var data = JSON.stringify();
var xhr = new XMLHttpRequest();
var url = "yoururl";
xhr.open = ("POST", url);
xhr.setRequestHeader("Content-Type", "text/plain");
xhr.send(data);