Try It Editor
Tip: Press
Ctrl/⌘ + Enter
to Run
▶ Run
Reset
Copy
Download
Code
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Form serialize() + $.ajax</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> </head> <body> <form id="f"> <input name="name" placeholder="Name"> <input name="email" placeholder="Email"> <button type="submit">Submit</button> </form> <pre id="out"></pre> <script> $("#f").on("submit", function(e){ e.preventDefault(); var data = $(this).serialize(); $.ajax({ url: "https://httpbin.org/post", method: "POST", data: data }).done(function(resp){ $("#out").text(JSON.stringify(resp, null, 2)); }).fail(function(){ $("#out").text("POST failed (network/CORS)."); }); }); </script> </body> </html>
Output