Files
30-seconds-of-code/snippets/http-delete.md
2023-04-28 22:29:23 +03:00

877 B

title, type, tags, cover, dateModified
title type tags cover dateModified
HTTP delete snippet
browser
beach-from-above 2020-10-19T22:49:51+03:00

Makes a DELETE request to the passed URL.

  • Use the XMLHttpRequest web API to make a DELETE request to the given url.
  • Handle the onload event, by running the provided callback function.
  • Handle the onerror event, by running the provided err function.
  • Omit the third argument, err to log the request to the console's error stream by default.
const httpDelete = (url, callback, err = console.error) => {
  const request = new XMLHttpRequest();
  request.open('DELETE', url, true);
  request.onload = () => callback(request);
  request.onerror = () => err(request);
  request.send();
};
httpDelete('https://jsonplaceholder.typicode.com/posts/1', request => {
  console.log(request.responseText);
}); // Logs: {}