AJAX form causes 404

by Piotr Stapp

HTML

<script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
<form id="loginForm" method="post">
  <input id="textinput" type="text" placeholder="login"/><br/>
  <input id="passwordinput" type="password" placeholder="password"/><br/>
  <button type="submit">Submit</button>
</form>

JavaScript

// attach to form submit
$("#loginForm").submit(function(e) {
    var url = "/echo/json"; // the url where you handle the form input.
    $.ajax({
           type: "POST",
           url: url,
           data: $("#loginForm").serialize(), // serializes the form's elements.
           success: function(data)
           {
               alert("You did it hurray :D"); // do stuff
           }
         });

    e.preventDefault(); // avoid to execute the actual submit of the form.
});