JavaScript & SPA Guide

AJAX Form Submission Without Writing a Backend

Modern React, Vue, Next.js, and Nuxt sites do not need jQuery or custom API routes to submit forms asynchronously. Send JSON directly to FormsPool with native fetch() and handle structured validation responses cleanly.

Native fetch() JSON submission pattern

Set Content-Type: application/json and Accept: application/json so FormsPool responds with machine-readable JSON instead of redirecting the browser. Always disable the submit button while the request is in flight to prevent duplicate entries.

const form = document.querySelector("#contact-form");
const submitBtn = form.querySelector("button[type=submit]");

form.addEventListener("submit", async (event) => {
  event.preventDefault();
  submitBtn.disabled = true;

  try {
    const payload = Object.fromEntries(new FormData(form));
    const response = await fetch("https://formspool.com/f/YOUR_FORM_ID", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "Accept": "application/json"
      },
      body: JSON.stringify(payload)
    });

    const result = await response.json();
    if (!response.ok) {
      // Display field-level errors returned by server validation
      console.warn(result.errors || result.message);
      return;
    }

    form.reset();
  } catch (networkErr) {
    console.error("Network error:", networkErr);
  } finally {
    submitBtn.disabled = false;
  }
});

Structured error codes you can map to UI states

Client-side HTML5 validation improves UX, but it is never a security boundary. FormsPool runs every AJAX payload through your server-side validation rules and returns predictable error codes when a field fails.

HTTP StatusError CodeMeaning
403INACTIVEForm is paused in Settings and not accepting submissions.
404FORM_NOT_FOUNDEndpoint ID does not match an active form.
422EMPTYRequest body contained no user fields.
422REQUIRED_FIELD_MISSINGA required field key was omitted from the JSON payload.
422REQUIRED_FIELD_EMPTYA required field was sent as an empty or whitespace-only string.
422TYPE_EMAIL / TYPE_NUMERICValue failed server-side type verification.
429RATE_LIMITEDToo many rapid requests from the same client IP.

Uploading files with FormData (avoid the Content-Type trap)

When a form includes file inputs or binary attachments, pass the FormData object directly as the fetch body and omit the Content-Type header. Setting multipart/form-data manually strips the browser-generated boundary token and causes the server parser to reject the payload.

const formData = new FormData(formElement);
const response = await fetch("https://formspool.com/f/YOUR_FORM_ID", {
  method: "POST",
  headers: {
    "Accept": "application/json"
    // Never set Content-Type manually for FormData
  },
  body: formData
});

Mapping HTTP 422 errors to inline field messages

Instead of showing a generic "something went wrong" banner, convert the errors array returned by FormsPool into a field-keyed map. That lets your React, Vue, or vanilla JS form highlight the exact invalid input right next to its label.

if (response.status === 422 && Array.isArray(result.errors)) {
  const fieldMap = {};
  for (const item of result.errors) {
    fieldMap[item.field] = item.message;
  }
  renderInlineErrors(fieldMap);
} else if (response.status === 429) {
  showBanner("Too many attempts. Please wait a minute and try again.");
}

Acceptance checklist before going live

Before shipping an AJAX form to production, verify seven cases: normal submission, missing required field, invalid email syntax, paused form response, rapid double-click protection, offline network failure, and CORS preflight from your production domain. You can inspect live payloads and response behavior inside the endpoint documentation or start from our HTML contact form templates.

Frequently Asked Questions

Why must I include Accept: application/json in AJAX form requests?

Including Accept: application/json tells FormsPool to return structured JSON responses (HTTP 200 on success or HTTP 422 with field error codes) instead of issuing an HTML 303 redirect.

Can I send file uploads via AJAX?

Yes. Submit a FormData instance without manually setting the Content-Type header so the browser generates the correct multipart/form-data boundary automatically.