POST Data
suggest changePosting form data
fetch(`/example/submit`, {
method: 'POST',
body: new FormData(document.getElementById('example-form'))
});
Posting JSON data. Set Content-Type yourself; fetch does not do that for a string body. FormData above does set multipart/form-data, including the boundary. fetch rejects on network failure, not on an HTTP error status, so check response.ok.
fetch("/example/submit.json", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email: document.getElementById("example-email").value,
comment: document.getElementById("example-comment").value
})
});
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents