I have a basic form in HTML that looks like this:
<form id="myForm">
<input type="text" id="name" name="name" />
<br />
<input type="text" id="email" name="email" />
<input type="button" id="myButton" onclick="sendIt();">Send It</input>
</form>
<script type="text/javascript">
function sendIt() {
var endpoint = '/send-it';
var xhr = new XMLHttpRequest();
xhr.open('POST', endpoint, true);
xhr.send();
}
</script>
In reality, my form is MUCH larger and has many more fields. I am trying to figure out how to POST this over AJAX. Previously, I was POSTing by using the method="POST" and an action on the form. However, I'm trying to remove the page refresh. So, I wanted to leverage AJAX via JQuery.
Is there some easy way to serialize the data and send it? I saw the following:
var data = $.param('#myForm');
But, that seems only relevant for appending to a query string. How can I POST myForm
to my service via AJAX with jQuery?
Thanks!