xxxxxxxxxx
//Using the javascript Fetch API
//References: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
fetch('http://localhost:8080/test')
.then((response) => response.json())
.then((data) => console.log(data));
xxxxxxxxxx
function httpGetAsync(theUrl, callback) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}
httpGetAsync("/api/v1/items", (res)=>{
// page content is in variable "res"
});
xxxxxxxxxx
const requests = new XMLHTTPRequest();
requests.open('METHOD', url)
requests.send()
requests.onload = () => {
if (requests.status == 200) {
console.log('ok')
} else {
console.log('didnt work')
}
}
xxxxxxxxxx
function httpGetAsync(url, callback) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", url, true); // true for asynchronous
xmlHttp.send(null);
}
xxxxxxxxxx
fetch("https://api.example.com/data") // Replace URL with your endpoint
.then(function(response) {
if (response.ok) {
return response.json();
}
throw new Error("Network response was not ok.");
})
.then(function(data) {
console.log(data); // Process the response data here
})
.catch(function(error) {
console.log("Error:", error);
});
xxxxxxxxxx
const xhr = new XMLHttpRequest();
xhr.open('GET', 'https://example.com/api/data');
xhr.onload = () => {
if (xhr.status === 200) {
xxxxxxxxxx
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://api.example.com/data', true);
xhr.onload = function() {
if (xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
// Process the response data here
}
};
xhr.onerror = function() {
// Handle any error that occurs during the request
};
xhr.send();
xxxxxxxxxx
fetch('https://api.example.com/data')
.then(function(response) {
if (response.ok) {
return response.json();
} else {
throw new Error('Error: ' + response.status);
}
})
.then(function(data) {
// Process the response data here
})
.catch(function(error) {
// Handle any error that occurs during the request
});
xxxxxxxxxx
const xmlRequest = new XMLHttpRequest()
//'open' method expects two arguments the first is http method and the second is url
xmlRequest.open('GET', 'http://example.com')
xmlRequest.send()
xmlRequest.addEventListener('readystatechange', (e)=>{
//the if statement checks that the response was completed (4) and that the status was successful (200)
if(e.target.readyState === 4 && e.target.status === 200){
const data = JSON.parse(e.target.textResponse)
console.log(data)
//else if clause would run when there was a bad status meaning an error occured
}else if(e.target.readyState === 4){
console.log('error')
}
})
xxxxxxxxxx
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://api.example.com/data", true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
// Handle the response data here
console.log(response);
}
};
xhr.send();