Un formulaire HTML amène le navigateur à rafraîchir la page ou à naviguer vers une nouvelle page. Pour envoyer les données du formulaire au point de terminaison de l’API, vous devez intercepter la soumission du formulaire à l’aide de JavaScript.
Cette méthode vous montre comment envoyer les données du formulaire au point de terminaison de l’API et comment traiter ces données.
Créez un point de terminaison API POST
à /api/feedback
qui recevra les données du formulaire. Utilisez request.formData()
pour les traiter. Assurez-vous de valider les valeurs du formulaire avant de les utiliser.
Cet exemple envoie un objet JSON avec un message au 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 " ) ;
// Valider les données - vous voudrez probablement faire plus que cela
if ( ! name || ! email || ! message) {
message: " Missing required fields " ,
// Faire quelque chose avec les données, puis renvoyer une réponse positive
Créez un composant de formulaire en utilisant votre framework UI. Chaque entrée doit avoir un attribut name
qui décrit la valeur de cette entrée.
Veillez à inclure un élément <button>
ou <input type="submit">
pour soumettre le formulaire.
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 / >
Créez une fonction qui accepte un événement submit, puis passez-la comme gestionnaire submit
à votre formulaire.
Dans la fonction :
Appelez preventDefault()
sur l’événement pour remplacer le processus de soumission par défaut du navigateur.
Créez un objet FormData
et envoyez-le dans une requête POST
à votre point de terminaison en utilisant 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 { FormEvent, useState } 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 >
Importez et incluez votre composant <FeedbackForm />
dans une page. Assurez-vous d’utiliser une directive client:*
pour garantir que la logique du formulaire est hydratée quand vous le souhaitez.
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