Skip to Content
Heels LogoHeelsDocs
Form Handling

AJAX & Modern Frameworks

Submit form events via JavaScript fetch, React, Next.js, Vue, and Svelte without page reloads.

AJAX & Modern Frameworks

For modern single-page applications (SPAs) and dynamic web apps, you can send asynchronous JSON payloads directly to Heels using fetch() or axios. This allows you to manage inline loading spinners, optimistic UI updates, and validation modals without page refreshes.


Vanilla JavaScript fetch

const HEELS_ENDPOINT = "https://publisher-heels.arshil.cc/events/YOUR_INTEGRATION_ID";

async function submitContactForm(data) {
  try {
    const response = await fetch(HEELS_ENDPOINT, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "Accept": "application/json",
      },
      body: JSON.stringify(data),
    });

    const result = await response.json();

    if (!response.ok) {
      throw new Error(result.error || "Submission failed");
    }

    console.log("Event queued with ID:", result.eventId);
    return result;
  } catch (err) {
    console.error("Failed to deliver to Heels:", err);
    throw err;
  }
}

Framework Examples

'use client';

import { useState } from 'react';

export function ContactWidget() {
  const [status, setStatus] = useState<'idle' | 'loading' | 'success' | 'error'>('idle');
  const [errorMessage, setErrorMessage] = useState('');

  async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
    event.preventDefault();
    setStatus('loading');
    setErrorMessage('');

    const form = event.currentTarget;
    const formData = new FormData(form);
    const payload = Object.fromEntries(formData.entries());

    try {
      const res = await fetch('https://publisher-heels.arshil.cc/events/YOUR_INTEGRATION_ID', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(payload),
      });

      if (!res.ok) {
        const errorData = await res.json();
        throw new Error(errorData.error || 'Failed to submit form');
      }

      setStatus('success');
      form.reset();
    } catch (err: any) {
      setStatus('error');
      setErrorMessage(err.message || 'An unexpected error occurred.');
    }
  }

  if (status === 'success') {
    return (
      <div className="p-4 bg-green-50 border border-green-300 rounded text-green-800">
        Thank you! Your message has been routed to our team.
      </div>
    );
  }

  return (
    <form onSubmit={handleSubmit} className="space-y-4">
      <input
        type="text"
        name="name"
        placeholder="Your Name"
        required
        disabled={status === 'loading'}
        className="border p-2 w-full rounded"
      />
      <input
        type="email"
        name="email"
        placeholder="[email protected]"
        required
        disabled={status === 'loading'}
        className="border p-2 w-full rounded"
      />
      <textarea
        name="message"
        placeholder="How can we help?"
        required
        rows={4}
        disabled={status === 'loading'}
        className="border p-2 w-full rounded"
      />

      {status === 'error' && (
        <p className="text-red-600 text-sm">{errorMessage}</p>
      )}

      <button
        type="submit"
        disabled={status === 'loading'}
        className="px-4 py-2 bg-yellow-400 font-bold border-2 border-black rounded shadow-[2px_2px_0px_rgba(0,0,0,1)] hover:translate-y-0.5"
      >
        {status === 'loading' ? 'Sending...' : 'Send Message'}
      </button>
    </form>
  );
}

On this page