title: How to send a form delete request with Node/Express and AJAX?
body: <p>I'm trying to build a REST API with NodeJs and Express (I'm a very beginner in programming), the CRUD requests work fine in Postman, but now I don't know how to send a DELETE request through a form with AJAX. I just want insert an id the text input and submit it to the server to delete a document in MongoDB. I searched everywhere but I could not understand how to perform this.</p>
  
  <p><strong>App.js</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>app.delete('/delete/:id', (req, res) =&gt; {
    let id = req.params.id;
    if (!ObjectID.isValid(id)) {
      return res.status(400).send();
    }
    Blog.findByIdAndRemove(id).then((docs) =&gt; {
      res.status(200).send({docs})
    }).catch((e) =&gt; {
      res.status(400)
    });
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>HTML</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>                                                   &lt;!DOCTYPE html&gt;
  &lt;html&gt;
    &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;title&gt;Titolo&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;form action='/delete/:id' method="POST"&gt;
        &lt;input id="id" type="text" name="id" /&gt;&lt;br /&gt;
        &lt;button id:"cancella" type="submit"&gt;Submit&lt;/button&gt;
      &lt;/form&gt;
  
      &lt;script src="/js/jquery.js"&gt;&lt;/script&gt;
      &lt;script src="/js/script.js"&gt;&lt;/script&gt;
    &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>script.js</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$(document).ready(function() {
   $('#cancella').on("click", function() {
    var id = $('#id').val();
    $.ajax({
     url: '/delete/:id',
     type: 'DELETE',
     data: {"id": id},
     dataType: 'json',
     success: function(data) {
      //do something
    }});
   });
  });</code></pre>
  </div>
  </div>
  </p>