0

How do I post or delete the specific data from the API? the most of the examples I have seen are either using PHP is there any to perform this using only ajax script

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    //console.log(this.response)
    var arrayOfObjects = eval(this.response);
    for (var i = 0; i < arrayOfObjects.length; i++) {
      var object = arrayOfObjects[i];
      document.getElementById("result").innerHTML += object.id + " " + object.name + " " + object.email + "<br/>";
    }

  }
};
xhttp.open("GET", "https://....mockapi.io/get", true);
xhttp.send();
<div id="demo"></div>
<div id="result"></div>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 1
    Why are you using `XMLHttpRequest`? There is [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) in new environments but even then you have jQuery tagged, so [`$.ajax`](https://api.jquery.com/jquery.ajax/) would be available. Which makes the problem non-existent `$.ajax(url, {method: "DELETE"})` – VLAZ Jul 03 '20 at 05:28
  • The server needs to support it too. I doubt you can use "delete" on `https://...mockapi.io/get` – mplungjan Jul 03 '20 at 05:31
  • Also relevant: [Javascript: Fetch DELETE and PUT requests](https://stackoverflow.com/q/40284338) – VLAZ Jul 03 '20 at 05:33
  • Also you HTML and script was not valid. You had html outside the body tag and trailing `}` in the script – mplungjan Jul 03 '20 at 05:34

0 Answers0