Build forms with API routes
このコンテンツはまだ日本語訳がありません。
An HTML form causes the browser to refresh the page or navigate to a new one. To send form data to an API endpoint instead, you must intercept the form submission using JavaScript.
This recipe shows you how to send form data to an API endpoint and handle that data.
Prerequisites
Section titled Prerequisites- A project with SSR (
output: 'server'
or'hybrid'
) enabled - A UI Framework integration installed
Recipe
Section titled Recipe-
Create a
POST
API endpoint at/api/feedback
that will receive the form data. Userequest.formData()
to process it. Be sure to validate the form values before you use them.This example sends a JSON object with a message back to the client.
-
Create a form component using your UI framework. Each input should have a
name
attribute that describes the value of that input.Be sure to include a
<button>
or<input type="submit">
element to submit the form. -
Create a function that accepts a submit event, then pass it as a
submit
handler to your form.In the function:
- Call
preventDefault()
on the event to override the browser’s default submission process. - Create a
FormData
object and send it in aPOST
request to your endpoint usingfetch()
.
- Call
-
Import and include your
<FeedbackForm />
component on a page. Be sure to use aclient:*
directive to ensure that the form logic is hydrated when you want it to be.