HTML 表单会导致浏览器刷新页面或导航到新页面。要将表单数据发送到 API 端点,你必须使用 JavaScript 拦截表单提交。
¥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.
本节向你展示如何将表单数据发送到 API 端点并处理该数据。
¥This recipe shows you how to send form data to an API endpoint and handle that data.
¥Prerequisites
¥Recipe
Create a POST
API endpoint at /api/feedback
that will receive the form data. Use request.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.
import type { APIRoute } from " astro " ;
export const POST : APIRoute = async ( { request } ) => {
const data = await request . formData () ;
const name = data . get ( " name " ) ;
const email = data . get ( " email " ) ;
const message = data . get ( " message " ) ;
// Validate the data - you'll probably want to do more than this
if ( ! name || ! email || ! message) {
message: " Missing required fields " ,
// Do something with the data, then return a success response
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.
export default function Form () {
< input type = " text " id = " name " name = " name " required />
< input type = " email " id = " email " name = " email " required />
< textarea id = " message " name = " message " required />
export default function Form () {
< input type = " text " id = " name " name = " name " required />
< input type = " email " id = " email " name = " email " required />
< textarea id = " message " name = " message " required />
export default function Form () {
< input type = " text " id = " name " name = " name " required />
< input type = " email " id = " email " name = " email " required />
< textarea id = " message " name = " message " required />
< input type = " text " id = " name " name = " name " required />
< input type = " email " id = " email " name = " email " required />
< textarea id = " message " name = " message " required />
< input type = " text " id = " name " name = " name " required />
< input type = " email " id = " email " name = " email " required />
< textarea id = " message " name = " message " required / >
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 a POST
request to your endpoint using fetch()
.
import { useState } from " preact/hooks " ;
export default function Form () {
const [ responseMessage , setResponseMessage ] = useState ( "" );
async function submit ( e : SubmitEvent ) {
const formData = new FormData (e . target as HTMLFormElement );
const response = await fetch ( " /api/feedback " , {
const data = await response . json ();
setResponseMessage (data . message );
< input type = " text " id = " name " name = " name " required />
< input type = " email " id = " email " name = " email " required />
< textarea id = " message " name = " message " required />
{ responseMessage && < p > { responseMessage } </ p > }
import { useState } from " react " ;
import type { FormEvent } from " react " ;
export default function Form () {
const [ responseMessage , setResponseMessage ] = useState ( "" );
async function submit ( e : FormEvent < HTMLFormElement > ) {
const formData = new FormData (e . target as HTMLFormElement );
const response = await fetch ( " /api/feedback " , {
const data = await response . json ();
setResponseMessage (data . message );
< input type = " text " id = " name " name = " name " autoComplete = " name " required />
< input type = " email " id = " email " name = " email " autoComplete = " email " required />
< label htmlFor = " message " >
< textarea id = " message " name = " message " autoComplete = " off " required />
{ responseMessage && < p > { responseMessage } </ p > }
import { createSignal, createResource, Suspense } from " solid-js " ;
async function postFormData ( formData : FormData ) {
const response = await fetch ( " /api/feedback " , {
const data = await response . json ();
export default function Form () {
const [ formData , setFormData ] = createSignal < FormData > ();
const [ response ] = createResource ( formData , postFormData);
function submit ( e : SubmitEvent ) {
setFormData ( new FormData (e . target as HTMLFormElement ));
< input type = " text " id = " name " name = " name " required />
< input type = " email " id = " email " name = " email " required />
< textarea id = " message " name = " message " required />
< Suspense > { response () && < p > { response () . message } </ p > } </ Suspense >
let responseMessage : string ;
async function submit ( e : SubmitEvent ) {
const formData = new FormData (e . currentTarget as HTMLFormElement );
const response = await fetch ( " /api/feedback " , {
const data = await response . json ();
responseMessage = data . message ;
< form on :submit= { submit } >
< input type = " text " id = " name " name = " name " required />
< input type = " email " id = " email " name = " email " required />
< textarea id = " message " name = " message " required />
import { ref } from " vue " ;
const responseMessage = ref < string > ();
async function submit ( e : Event ) {
const formData = new FormData (e . currentTarget as HTMLFormElement );
const response = await fetch ( " /api/feedback " , {
const data = await response . json ();
responseMessage . value = data . message ;
< input type = " text " id = " name " name = " name " required />
< input type = " email " id = " email " name = " email " required />
< textarea id = " message " name = " message " required / >
< p v-if = " responseMessage " > {{ responseMessage }} </ p >
Import and include your <FeedbackForm />
component on a page. Be sure to use a client:*
directive to ensure that the form logic is hydrated when you want it to be.
import FeedbackForm from " ../components/FeedbackForm "
< FeedbackForm client:load />
import FeedbackForm from " ../components/FeedbackForm "
< FeedbackForm client:load />
import FeedbackForm from " ../components/FeedbackForm "
< FeedbackForm client:load />
import FeedbackForm from " ../components/FeedbackForm.svelte "
< FeedbackForm client:load />
import FeedbackForm from " ../components/FeedbackForm.vue "
< FeedbackForm client:load />
Recipes