AJAX
: Stands for Asynchronous Javascript and XML.Classic Full Page Reloads
At a high level, AJAX is simply a group of different technologies that work together to allow a website to communicate with a server in the bg without requiring the website to need to reload.
Nowadays, XML has more or less been replaced by JSON.
AJAX introduces more complexity but the benefit is an improved user experience.
AJAX allows you to keep the user in their current context.
There are JS libraries that make using AJAX a lot easier (i.e. JQuery)
Fetch
is used to make HTTP Requests, it uses Promises to handle the async nature of HTTP requests and responses.GET
: Request used to retrieve information from the server.fetch("https://jservice.xyz/api/games")
.then(function (res) {
console.log("response: ", res);
return res.json();
})
.then(function (data) {
console.log("data:", data);
});
options
argument, which can be used to add additional header information.POST
: Used to create data on the server.fetch("https://jservice.xyz/api/categories", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
title: "ACTORS & THEIR FILMS",
}),
})
.then(function (res) {
console.log(res);
if (!res.ok) {
throw Error(res.statusText);
}
return res.json();
})
.then(function (data) {
console.log(data);
})
.catch(function (error) {
console.log(error);
});
ok
key inside of the Response Object, and if the key is false then the fetch can properly handle the error.ok
key and place a catch error handler at the end of the promise chain.AJAX Broken Down
<button class="want-to-read">Want to Read</button>
<script>
document.querySelector(".want-to-read").addEventListener("click", function() {
fetch(`https://api.goodreads.com/books/${BOOK_ID}/update-status`, {
method: "PATCH", // using PATCH since we'll just be modifying the book's status
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
status: "Want to Read"
})
})
.then(function(res) {
if (!res.ok) {
throw Error(res.statusText); // handle any potential server errors
}
return res.json(); // extract JSON from the response
})
.then(function(data) {
document.querySelector(".want-to-read").innerHTML = "✓ Want To Read";
})
.catch(function(error) {
const errorMessage = document.createElement("p");
errorMessage.appendChild(
document.createTextNode("Something went wrong. Please try again!")
);
// This example appends an error message to the body for simplicity's sake.
// Please do not copy this kind of DOM manipulation in your own projects:
document.querySelector("body").appendChild(errorMessage);
});
});
</script>
When we first send out the event listener and fetch with filled out options this is ths segment where we are conducting the Javascript Call
.
When the request is sent out it is the arrow leading from the AJAX engine to the Web Server.
The arrow from the Web Server back to the AJAX engine is the response from the Server in either XML or JSON format.
The response is handled within the AJAX engine and returns the new HTML & CSS for the UI.
ok
response is received so that potentials errors can be handled correctly.