JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html>
<body>
<button id="button">Send Request</button>
<p id="demo"></p>
</body>
</html>
JavaScript
document.getElementById("button").addEventListener("click", function(e) {
let first = 10;
let second = 20;
fetch('http://127.0.0.1/add?a=' + first + '&b=' + second)
.then((response) => {
return response.json();
})
.then((myJson) => {
document.getElementById("demo").innerHTML = "When I add " + first + " and " + second + " I get: " + myJson.result;
}).catch((error) => {
let fakeJSON = {
result: first + second
} // Only added to simulate
document.getElementById("demo").innerHTML = "When I add " + first + " and " + second + " I get: " + fakeJSON.result;
});
});