smartformify
Pricing
Browser fetch

Submit a Website Form With JavaScript

Use fetch when the form should stay on the current page. Send named fields as JSON, then control the loading, error, redirect, and confirmation states.

Create EndpointView Fetch Example

Request contract

Method

POST

Content-Type

application/json

Payload

{ data: fields }

Success

HTTP 201

Implementation

Keep the request inside the form handler

SmartFormify returns the JSON response for the fetch request. Read its success, message, redirect URL, and thank-you content before updating the page.

JavaScript fetch request

javascript

This example reads the JSON returned by SmartFormify, follows its configured redirect, and shows its message when the request fails.

const form = document.querySelector("#contact-form");
const status = document.querySelector("#form-status");

form.addEventListener("submit", async (event) => {
  event.preventDefault();
  status.textContent = "Sending...";

  const data = Object.fromEntries(new FormData(form).entries());

  try {
    const response = await fetch("https://api.smartformify.com/fe/YOUR_ENDPOINT_KEY", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ data })
    });

    const result = await response.json();
    if (!response.ok || !result.success) {
      throw new Error(result.message || "Submission failed.");
    }

    if (result.data.redirect_url) {
      window.location.assign(result.data.redirect_url);
      return;
    }

    status.innerHTML = result.data.thank_you_content;
    form.reset();
  } catch (error) {
    status.textContent = error.message;
  }
});

A clear request lifecycle

01

Validate

Run browser validation and keep the submitted values available.

02

Send

Disable the button and POST the fields inside the data object.

03

Resolve

Redirect, show thank-you content, or display the returned error.

Response handling

Read the status before changing the UI

SmartFormify returns the status and JSON data. A failed request should keep the form values in place. Reset the form only after the endpoint accepts the submission.

  • Use response.ok and result.success
  • Show result.message when useful
  • Read redirect_url before thank_you_content

201

Saved

Read redirect_url or thank_you_content.

401

Invalid key

Check the endpoint URL copied from SmartFormify.

403

Not allowed

Check endpoint availability and allowed domains.

429

Limited

Wait before retrying the request.