[JavaScript] Understanding XMLHttpRequest
GET, POST, PUT, DELETE methods to request the data and get the response back from the server.
![[JavaScript] Understanding XMLHttpRequest](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1683385984685%2Fe36d3b46-b6e4-4cd8-afc1-d0ba5fac1beb.jpeg&w=3840&q=75)
Computer Science Enthusiast with a Drive for Excellence | Data Science | Web Development | Passionate About Tech & Innovation
Introduction
This article will talk about:
GET to retrieve the data
POST to send the data.
PUT to replace the data.
DELETE to delete the data.
*note. I am using the JSON-server in this article.
GET Request
You can use the GET method to retrieve the data from the server.
function getData(url) {
return new Promise((resolve,reject)) => {
const xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.setRequestHeader("content-type", "application/json");
xhr.send();
xhr.onload = () => {
// if xhr.status === 200, it means retrieving the data was successful.
if (xhr.status === 200) {
// The string type needs to be converted into JSON type.
const res = JSON.parse(xhr.response);
resolve(res);
} else {
// prints the status code and status text if failed to retrieve the data.
console.log(xhr.status, xhr.statusText)
reject(xhr.status);
};
}
}
// prints out the corresponding data.
getData("http://localhost:3000/posts").then((res) => {
console.log(res);
});
POST
You can use the POST method to send the data to the server.
function postData() {
const xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:3000/posts");
xhr.setRequestHeader("content-type", "application/json;charset=UTF-8");
// specifies the send() because you are trying to send the data.
xhr.send(JSON.stringify(data)); // converts JSON data type to String.
xhr.onload = () => {
// if xhr.status === 201, it means sending the data was successful.
if (xhr.status === 201) {
// The string type needs to be converted into JSON type.
const res = JSON.parse(xhr.response);
console.log(res);
} else {
// prints the status code and status text if failed to retrieve the data.
console.log(xhr.status, xhr.statusText);
};
}
}
PUT
You can use the PUT method to replace the data.
function postData() {
const xhr = new XMLHttpRequest();
// I put 2 at the back of the URL to specify that I want to replace the data with ID number = 2.
xhr.open("PUT", "http://localhost:3000/posts/2");
xhr.setRequestHeader("content-type", "application/json;charset=UTF-8");
// The data you want to replace with.
const data = { title: "HTML", author: "John Doe" };
// specifies the send() because you are trying to send the data.
xhr.send(JSON.stringify(data)); // converts JSON data type to String.
xhr.onload = () => {
// if xhr.status === 200, it means replacing the data was successful.
if (xhr.status === 200) {
// The string type needs to be converted into JSON type.
const res = JSON.parse(xhr.response);
console.log(res);
} else {
// prints the status code and status text if failed to retrieve the data.
console.log(xhr.status, xhr.statusText);
};
}
}
DELETE
function postData() {
const xhr = new XMLHttpRequest();
// I put 2 at the back of the URL to specify that I want to delete the data with ID number = 2.
xhr.open("DELETE", "http://localhost:3000/posts/2");
xhr.setRequestHeader("content-type", "application/json;charset=UTF-8");
// The data you want to replace with.
const data = { title: "HTML", author: "John Doe" };
// specifies the send() because you are trying to send the data.
xhr.send(JSON.stringify(data)); // converts JSON data type to String.
xhr.onload = () => {
// if xhr.status === 200, it means deleting the data was successful.
if (xhr.status === 200) {
// The string type needs to be converted into JSON type.
const res = JSON.parse(xhr.response);
console.log(res);
} else {
// prints the status code and status text if failed to retrieve the data.
console.log(xhr.status, xhr.statusText);
};
}
}
![[CS fundamentals] Linked List Creation & Insertion explained in C.](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fuploads%2Fcovers%2F644c253f17f6efe1e02ade41%2F6a74e5c0-44ca-4110-9ee7-e8e67767a9dc.png&w=3840&q=75)
![[CS Fundamentals] Pointers in C](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fuploads%2Fcovers%2F644c253f17f6efe1e02ade41%2F6f81f5a6-d87c-4382-883f-fa4d0a8b5968.png&w=3840&q=75)
![[SQL] Writing Efficient SQL Queries](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1751292603168%2Fb0b43c7d-8e08-47b2-a64c-b600e3511831.jpeg&w=3840&q=75)
![[CS Fundamentals] Binary Search](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1747041878054%2Fc8d8adb9-097d-484b-9562-f43e4bce9a46.png&w=3840&q=75)
![[CS Fundamentals] Sorting Algorithms](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1746973590876%2Ffab37371-1496-42ef-950b-432e7637c89e.png&w=3840&q=75)