DELETE request using XMLHttpRequest by making Custom HTTP library

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:

  1. A function easyHTTP is first made and a new XMLHttpRequest() object is initialized.
  2. A new prototype is created for the delete method using easyHTTP.prototype.delete. It contains two parameters url and a callback.
  3. 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.
  4. 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.
  5. The last step is to send the request using the send() function.

Creating the app.js file:

  1. We will first instantiate easyHTTP that is created earlier using the new keyword.
  2. We then pass the URL and a callback function in the delete prototype function.
  3. 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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Code for library.js

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

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();
};

chevron_right


Code for app.js

Javascript



filter_none

edit
close

play_arrow

link
brightness_4
code

// Instantiate easyHTTP
const http = new easyHTTP();
  
// Use the delete prototype
// method with (URL, callback(error, response text))
 function (
  err,
  response
) {
  if (err) {
    console.log(err);
  } else {
    console.log(response);
  }
});

chevron_right


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.

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.