JavaScript

Submit forms programmatically with fetch for more control over the experience.

Basic Submission

const response = await fetch("https://airform.jossware.com/submit/YOUR_SITE_ID", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "Accept": "application/json"
  },
  body: JSON.stringify({
    Name: "Jane Doe",
    Email: "jane@example.com",
    Message: "Hello from JavaScript!"
  })
});

const data = await response.json();

if (data.success) {
  console.log("Submitted! ID:", data.submission_id);
} else {
  console.error("Error:", data.message);
}

Important: Set the Accept Header

When you want a JSON response, include Accept: application/json in your request. Without it, AirForm returns an HTML redirect response (designed for standard form submissions).

Response Format

Success (200):

{
  "success": true,
  "submission_id": 42
}

Error (4xx/5xx):

{
  "success": false,
  "error": "forbidden",
  "message": "Origin not allowed for this site"
}

With a Form Element

You can intercept a regular HTML form and submit it via JavaScript:

document.querySelector("form").addEventListener("submit", async (e) => {
  e.preventDefault();

  const formData = new FormData(e.target);
  const data = Object.fromEntries(formData);

  const response = await fetch(e.target.action, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "Accept": "application/json"
    },
    body: JSON.stringify(data)
  });

  const result = await response.json();

  if (result.success) {
    e.target.reset();
    alert("Thanks for your submission!");
  } else {
    alert("Something went wrong: " + result.message);
  }
});

CORS

AirForm handles CORS automatically. Requests are allowed from any domain you've added to your site configuration. The POST /submit/{site_id} endpoint responds to preflight OPTIONS requests with the appropriate Access-Control-Allow-Origin header.