I must be making a very stupid mistake, but I can't figure out why this isn't working:
<html>
<head>
<title>Form test</title>
</head>
<body>
<h1>Test a form</h1>
<form id="myform" name="myform" action="/server">
<input type="text" name="username" value="johndoe">
<input type="number" name="id" value="123456">
<input type="submit" onclick="return sendForm(this.form);">
</form>
<script>
function sendForm(form) {
var formData = new FormData(form);
console.log("as json: " + JSON.stringify(formData));
return false; // Prevent page from submitting.
}
</script>
</body>
</html>
According to the documentation of FormData, if you give it a form object in the constructor, it will populate itself with the form values, but when I do this, I get an empty result in the console. Any ideas what I'm doing wrong in this? I tried in both Chrome and Firefox and got the same results. This is based on the example from html5rocks XHR2.
Thank you