JavaScript
JavaScript is a high-level, versatile programming language primarily used to create dynamic and interactive features on websites. It is a core technology of the web, alongside HTML and CSS, and enables both client-side and server-side development.
Prerequisites
Before you begin, make sure you have:
- A Basestack Forms account
- A JavaScript project with a form component
Quick Setup Guide
Follow these steps to submit form data using JavaScript:
Choose your HTTP client
You can use any HTTP client library to send form data to Basestack Forms. Popular options include:
- Fetch API - Built into modern browsers, no installation needed
- Axios - Popular HTTP client library with additional features
- Any other HTTP client that supports POST requests
Make sure to include mode=rest in your endpoint URL query string to receive responses in JSON format. This allows you to handle the response programmatically.
Implement form submission
Copy the example code below into your JavaScript file and customize it for your form structure. The example uses the Fetch API, but you can adapt it to use Axios or any other HTTP client.
try {
const res = await fetch(
"https://forms-api.basestack.co/v1/s/[KEY]?mode=rest",
{
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({ name: "John Doe", message: "Hello World" }),
},
);
const data = await res.json();
if (data.code === 200) {
console.log("Form submitted successfully");
}
} catch (error) {
console.log("Error submitting form", error);
}