# AJAX and fetch

Submit forms asynchronously and handle the response in JavaScript — no page redirect.

## Fetch API (JSON body)

```js
const response = await fetch(
  'https://app.paceforms.com/api/f/NR-6Ww88ET',
  {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      name: 'Alice',
      email: 'alice@example.com',
      message: 'Hello from fetch!',
    }),
  }
);
const data = await response.json();
if (response.ok) {
  console.log('Submitted:', data);  // { id: 'sub_abc123', status: 'ok' }
} else {
  console.error('Error:', data.error);
}
```

## Fetch API (FormData — includes file uploads)

```js
const form = document.getElementById('my-form');
form.addEventListener('submit', async (e) => {
  e.preventDefault();
  const formData = new FormData(form);
  const response = await fetch(
    'https://app.paceforms.com/api/f/NR-6Ww88ET',
    { method: 'POST', body: formData }
  );
  const data = await response.json();
  if (response.ok) {
    form.innerHTML = '<p>Thanks! We\'ll be in touch.</p>';
  } else {
    alert('Something went wrong. Please try again.');
  }
});
```

The answers are listed in [Your endpoint](https://paceforms.com/docs/form-api/your-endpoint#response-format).
