---
title: Form Submissions with AJAX
date: '2021-06-30T13:03:31+00:00'
url: https://support.formkeep.com/formkeep-guide-to-form-submissions-with-ajax/
summary: "If you want more control over your FormKeep form, you may want to consider
  using JavaScript to submit. This may be a good idea if you are already using a JavaScript
  framework, you want to add validation logic, or you don’t want to redirect the user
  after they submit the form.\n\nCross Origin Requests are possible by submitting
  the form using Javascript.\n\nYou can submit to FormKeep using a standard AJAX request.
  To ensure the request does not cause a redirect, be sure to set the Accept header
  to application/javascript.\n\njQuery Example\n\nThe main thing to remember when
  using jQuery, is to set the Accept header to application/javascript. Also make sure
  to replace the “exampletoken” with your real token.\n&lt;html&gt;\n  &lt;head&gt;\n
  \   &lt;meta charset=\"UTF-8\" /&gt;\n    &lt;script src=\"https://unpkg.com/jquery/dist/jquery.js\"&gt;&lt;/script&gt;\n
  \ &lt;/head&gt;\n  &lt;body&gt;\n    &lt;form id=\"newsletter-signup\"\n          action=\"https://formkeep.com/f/exampletoken\"\n
  \         method=\"POST\"\n          accept-charset=\"UTF-8\"&gt;\n      &lt;input
  type=\"hidden\" name=\"utf8\" value=\"✓\"&gt;\n      &lt;input type=\"email\" name=\"email\"&gt;\n
  \     &lt;input value=\"Submit\" type=\"submit\"&gt;\n    &lt;/form&gt;\n\n    &lt;script
  type=\"text/javascript\"&gt;\n    $(function() {\n      $('#newsletter-signup').submit(function(event)
  {\n        event.preventDefault();\n\n        var formElement = $(this);\n        var
  submitButton = $('input[type=submit]', formElement);\n\n        $.ajax({\n          type:
  'POST',\n          url: formElement.prop('action'),\n          accept: {\n            javascript:
  'application/javascript'\n          },\n          data: formElement.serialize(),\n
  \         beforeSend: function() {\n            submitButton.prop('disabled', 'disabled');\n
  \         }\n        }).done(function(data) {\n          submitButton.prop('disabled',
  false);\n        });\n      });\n    });\n    &lt;/script&gt;\n  &lt;/body&gt;\n&lt;/html&gt;\n\n\n\nAxios
  Example\n\nAxios is very similar, but setting the Accept header can trip people
  up, see the config setup below.\nAlso make sure to replace the “exampletoken” with
  your real token.\n\n&lt;html&gt;\n  &lt;head&gt;\n    &lt;meta charset=\"UTF-8\"
  /&gt;\n    &lt;script src=\"https://unpkg.com/axios/dist/axios.min.js\" async&gt;&lt;/script&gt;\n
  \ &lt;/head&gt;\n  &lt;body&gt;\n    &lt;form id=\"newsletter-signup\"\n          action=\"https://formkeep.com/f/exampletoken\"\n
  \         method=\"POST\"\n          accept-charset=\"UTF-8\"&gt;\n      &lt;input
  type=\"hidden\" name=\"utf8\" value=\"✓\"&gt;\n      &lt;input type=\"email\" name=\"email\"&gt;\n
  \     &lt;input value=\"Submit\" type=\"submit\"&gt;\n    &lt;/form&gt;\n\n    &lt;script
  type=\"text/javascript\"&gt;\n      // Function to handle form submission\n      function
  handleSubmit(event) {\n        event.preventDefault(); // Prevent default form submission
  behavior\n\n        // Get the form element\n        const form = document.getElementById('newsletter-signup');\n\n
  \       // Get the form data\n        const formData = new FormData(form);\n\n        const
  config = {\n          headers: {\n            'Accept': 'application/javascript',
  // Set the Accept header to JSON\n          }\n        };\n\n        // Make a POST
  request using Axios\n        axios.post(form.action, formData, config)\n          .then(function
  (response) {\n            // Handle the successful response\n            console.log(response);\n
  \         })\n          .catch(function (error) {\n            // Handle the error\n
  \           console.error(error);\n          });\n      }\n\n      // Add event
  listener to the form's submit event\n      document.getElementById('newsletter-signup').addEventListener('submit',
  handleSubmit);\n    &lt;/script&gt;\n  &lt;/body&gt;\n&lt;/html&gt;\n\n\n\n\n\nJavascript
  fetch Example\n\nJavascript fetch is very similar, but setting the no-cors mode
  or not setting the accept-header can trip people up, see the config setup below.
  Also make sure to replace the “exampletoken” with your real token.\n\nOne note is
  to make sure NOT to include the no-cors mode, if you do we’ll still get the data
  on our side, but your javascript won’t be able to get a valid response back.\n\nFrom
  the docs around fetch(): But when using fetch with mode: “no-cors”, the browser
  prevents you from being able to read anything from the resulting Response object
  or view the status, thus, response.ok is always false, and response.status is always
  set to 0. This is a security feature.\n\n&lt;html&gt;\n  &lt;head&gt;\n    &lt;meta
  charset=\"UTF-8\" /&gt;\n  &lt;/head&gt;\n  &lt;body&gt;\n    &lt;form id=\"newsletter-signup\"\n
  \         action=\"https://formkeep.com/f/exampletoken\"\n          method=\"POST\"\n
  \         accept-charset=\"UTF-8\"&gt;\n      &lt;input type=\"hidden\" name=\"utf8\"
  value=\"✓\"&gt;\n      &lt;input type=\"email\" name=\"email\"&gt;\n      &lt;input
  value=\"Submit\" type=\"submit\"&gt;\n    &lt;/form&gt;\n\n    &lt;script type=\"text/javascript\"&gt;\n
  \     // Function to handle form submission\n      function handleSubmit(event)
  {\n        event.preventDefault(); // Prevent default form submission behavior\n\n
  \       // Get the form element\n        const form = document.getElementById('newsletter-signup');\n\n
  \       // Get the form data\n        const formData = new FormData(form);\n\n        //
  Make a POST request using javascript fetch() api\n        fetch(form.action, {\n
  \         method: 'POST',\n          body: formData,\n          headers: {\n            'Accept':
  'application/javascript',\n          },\n        }).then(function (response) {\n
  \         // Handle the successful response, note we're not going to send anything
  back other than the ok status, if you've set the no-cors this is going to fail\n
  \         console.log(response.ok);\n        })\n        .catch(function (error)
  {\n          // Handle the error\n          console.error(error);\n        });\n
  \     }\n\n      // Add event listener to the form's submit event\n      document.getElementById('newsletter-signup').addEventListener('submit',
  handleSubmit);\n    &lt;/script&gt;\n  &lt;/body&gt;\n&lt;/html&gt;\n\n\n\n## formkeep.js
  library Example\n\nGiven the following form:\n\n~~~html\n\n  \n  \n  \n\n~~~\n\nYou
  can display your own UX or message when the submission is complete. We've built
  a small js library that can make this super simple and have also provided some examples
  using common js libraries.\n\nThe source lives at [https://github.com/furiouscollective/formkeep.js](https://github.com/furiouscollective/formkeep.js)\nOr
  the npm package here [https://www.npmjs.com/package/@formkeep/formkeep](https://www.npmjs.com/package/@formkeep/formkeep)\nCheck
  out the [documentation](https://github.com/furiouscollective/formkeep.js/blob/master/README.md),
  but something simple to get you started\n\n~~~html\n\n  FormKeep.post('', { hello:
  'world' })\n~~~\n -->"
tags:
- Settings
- Guides
author: Support Team
---

# Form Submissions with AJAX

If you want more control over your FormKeep form, you may want to consider using JavaScript to submit. This may be a good idea if you are already using a JavaScript framework, you want to add validation logic, or you don't want to redirect the user after they submit the form.

Cross Origin Requests are possible by submitting the form using Javascript.

You can submit to FormKeep using a standard AJAX request. To ensure the request does not cause a redirect, be sure to set the **Accept** header to **application/javascript**.

### jQuery Example

The main thing to remember when using [jQuery](https://jquery.com/), is to set the Accept header to **application/javascript**. Also make sure to replace the "exampletoken" with your real token.
~~~html
<html>
  <head>
    <meta charset="UTF-8" />
    <script src="https://unpkg.com/jquery/dist/jquery.js"></script>
  </head>
  <body>
    <form id="newsletter-signup"
          action="https://formkeep.com/f/exampletoken"
          method="POST"
          accept-charset="UTF-8">
      <input type="hidden" name="utf8" value="✓">
      <input type="email" name="email">
      <input value="Submit" type="submit">
    </form>

    <script type="text/javascript">
    $(function() {
      $('#newsletter-signup').submit(function(event) {
        event.preventDefault();

        var formElement = $(this);
        var submitButton = $('input[type=submit]', formElement);

        $.ajax({
          type: 'POST',
          url: formElement.prop('action'),
          accept: {
            javascript: 'application/javascript'
          },
          data: formElement.serialize(),
          beforeSend: function() {
            submitButton.prop('disabled', 'disabled');
          }
        }).done(function(data) {
          submitButton.prop('disabled', false);
        });
      });
    });
    </script>
  </body>
</html>
~~~
<br>

## Axios Example

[Axios](https://github.com/axios/axios) is very similar, but setting the Accept header can trip people up, see the config setup below.
Also make sure to replace the "exampletoken" with your real token.

~~~html
<html>
  <head>
    <meta charset="UTF-8" />
    <script src="https://unpkg.com/axios/dist/axios.min.js" async></script>
  </head>
  <body>
    <form id="newsletter-signup"
          action="https://formkeep.com/f/exampletoken"
          method="POST"
          accept-charset="UTF-8">
      <input type="hidden" name="utf8" value="✓">
      <input type="email" name="email">
      <input value="Submit" type="submit">
    </form>

    <script type="text/javascript">
      // Function to handle form submission
      function handleSubmit(event) {
        event.preventDefault(); // Prevent default form submission behavior

        // Get the form element
        const form = document.getElementById('newsletter-signup');

        // Get the form data
        const formData = new FormData(form);

        const config = {
          headers: {
            'Accept': 'application/javascript', // Set the Accept header to JSON
          }
        };

        // Make a POST request using Axios
        axios.post(form.action, formData, config)
          .then(function (response) {
            // Handle the successful response
            console.log(response);
          })
          .catch(function (error) {
            // Handle the error
            console.error(error);
          });
      }

      // Add event listener to the form's submit event
      document.getElementById('newsletter-signup').addEventListener('submit', handleSubmit);
    </script>
  </body>
</html>

~~~

<br>

## Javascript fetch Example

[Javascript fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) is very similar, but setting the no-cors mode or not setting the accept-header can trip people up, see the config setup below. Also make sure to replace the "exampletoken" with your real token.

One note is to make sure NOT to include the no-cors mode, if you do we'll still get the data on our side, but your javascript won't be able to get a valid response back.

From the docs around fetch(): But when using fetch with mode: "no-cors", the browser prevents you from being able to read anything from the resulting Response object or view the status, thus, response.ok is always false, and response.status is always set to 0. This is a security feature.

~~~html
<html>
  <head>
    <meta charset="UTF-8" />
  </head>
  <body>
    <form id="newsletter-signup"
          action="https://formkeep.com/f/exampletoken"
          method="POST"
          accept-charset="UTF-8">
      <input type="hidden" name="utf8" value="✓">
      <input type="email" name="email">
      <input value="Submit" type="submit">
    </form>

    <script type="text/javascript">
      // Function to handle form submission
      function handleSubmit(event) {
        event.preventDefault(); // Prevent default form submission behavior

        // Get the form element
        const form = document.getElementById('newsletter-signup');

        // Get the form data
        const formData = new FormData(form);

        // Make a POST request using javascript fetch() api
        fetch(form.action, {
          method: 'POST',
          body: formData,
          headers: {
            'Accept': 'application/javascript',
          },
        }).then(function (response) {
          // Handle the successful response, note we're not going to send anything back other than the ok status, if you've set the no-cors this is going to fail
          console.log(response.ok);
        })
        .catch(function (error) {
          // Handle the error
          console.error(error);
        });
      }

      // Add event listener to the form's submit event
      document.getElementById('newsletter-signup').addEventListener('submit', handleSubmit);
    </script>
  </body>
</html>

~~~
<!-- <br>
## formkeep.js library Example

Given the following form:

~~~html
<form id="newsletter-signup" action="https://formkeep.com/f/exampletoken" method="POST" accept-charset="UTF-8">
  <input type="hidden" name="utf8" value="✓">
  <input type="email" name="email">
  <input value="Submit" type="submit">
</form>
~~~

You can display your own UX or message when the submission is complete. We've built a small js library that can make this super simple and have also provided some examples using common js libraries.

The source lives at [https://github.com/furiouscollective/formkeep.js](https://github.com/furiouscollective/formkeep.js)
Or the npm package here [https://www.npmjs.com/package/@formkeep/formkeep](https://www.npmjs.com/package/@formkeep/formkeep)
Check out the [documentation](https://github.com/furiouscollective/formkeep.js/blob/master/README.md), but something simple to get you started

~~~html
<script src="unpkg.com/@formkeep/formkeep"></script>
  FormKeep.post('<YOUR_FORM_IDENTIFIER>', { hello: 'world' })
~~~
<br> -->
