DELETE request using XMLHttpRequest by making Custom HTTP library Last Updated : 22 Jul, 2020 Comments Improve Suggest changes Like Article Like Report The task here is to show how the XMLHttpRequest can be used to DELETE data to an API by making a custom HTTP library. A placeholder API that contains an array of objects would be used as an example. The DELETE request is performed on this API. The URL of the API is https://jsonplaceholder.typicode.com/posts/ Prerequisites: Basic knowledge of HTML, CSS, and JavaScript is required.An HTML file has to be created where the basic HTML markup is added as shown in the example below. At the end of the body, we attach two scripts which are library.js and app.js in the respective order. We will then created these files.Creating the library.js file:A function easyHTTP is first made and a new XMLHttpRequest() object is initialized.A new prototype is created for the delete method using easyHTTP.prototype.delete. It contains two parameters url and a callback.A new request is opened using the open method. It takes three parameters, the first one is the type of the request (GET, POST, PUT or DELETE), the second is the URL for the API and last one is a boolean value, where true means an asynchronous call and false means synchronous call.The onload handler is used to display the data. It is executed after the API call is done. The status of the response is checked. If the status code is 200 then a callback function is run which contains two arguments, error and response text. If status code is not 200 then the callback function will simply print the error message.The last step is to send the request using the send() function.Creating the app.js file:We will first instantiate easyHTTP that is created earlier using the new keyword.We then pass the URL and a callback function in the delete prototype function.The callback function will contain two arguments, that is, error to print if any error occurs and response to get the actual response. The code examples below demonstrate the creation of all the required files. Code for index.html HTML <!DOCTYPE html> <html lang="en"> <head> <title>Delete request</title> </head> <body> <h1> Delete request using xmlhttpRequest/ Ajax by making custom HTTP library. </h1> <!-- Including library.js and app.js files --> <script src="library.js"></script> <script src="app.js"></script> </body> </html> Code for library.js JavaScript function easyHTTP() { // Initialising new XMLHttpRequest method. this.http = new XMLHttpRequest(); } // Make an HTTP Delete Request easyHTTP.prototype.delete = function (url, callback) { // Open an request (GET/POST/PUT/DELETE, // PATH, ASYNC - TRUE/FALSE) this.http.open("DELETE", url, true); // Assigning this to self to have // scope of this into the function let self = this; // When the response is ready this.http.onload = function () { // Checking status if (self.http.status === 200) { // Callback function (Error, response text) callback(null, "Post Deleted"); } else { // Callback function (Error message) callback("Error: " + self.http.status); } }; // Send the request this.http.send(); }; Code for app.js JavaScript // Instantiate easyHTTP const http = new easyHTTP(); // Use the delete prototype // method with (URL, callback(error, response text)) http.delete("https://jsonplaceholder.typicode.com/posts/1", function ( err, response ) { if (err) { console.log(err); } else { console.log(response); } }); Output: Open the index.html file in any browser and open the developer console. The output of the program would be visible here. The DELETE request along with its details can also be observed by using the Networks tab. Comment More infoAdvertise with us Next Article DELETE request using XMLHttpRequest by making Custom HTTP library T thacker_shahid Follow Improve Article Tags : JavaScript Web Technologies HTML HTML5 JavaScript-Misc +1 More Similar Reads JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read HTML Tutorial HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript 11 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as 15+ min read React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon 8 min read Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w 8 min read NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net 15+ min read HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML 14 min read What is an API (Application Programming Interface) In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of 10 min read Like