handling response from POST in +page.svelte #6079
Unanswered
stukennedy
asked this question in
Q&A
Replies: 1 comment 4 replies
-
You can redirect to the same URL with a query param like +page.svelte <script lang="ts">
import { goto } from '$app/navigation';
import { page } from '$app/stores';
async function handleSubmit(this: HTMLFormElement) {
const res = await fetch(this.action, {
headers: {
Accept: 'application/json'
},
method: this.method,
body: new FormData(this),
credentials: 'include'
});
if (res.ok) {
goto('?success');
}
}
</script>
{#if $page.url.searchParams.has('success')}
<h1>Success! Check your email inbox</h1>
{:else}
<form method="post" on:submit|preventDefault={handleSubmit}>
<input type="email" name="email" placeholder="Email" />
<button type="submit">Submit</button>
</form>
{/if} +page.server.ts import type { Action } from './$types';
export const POST: Action = async ({ request, url }) => {
const formData = await request.formData();
const email = formData.get('email') as string;
// do the supabase stuff
console.log(email);
return {
location: url.pathname + '?success'
};
}; |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
previously I could return data from my
POST
shadow endpoint method, which meant I could display a success box in my page.With the latest changes came the removal of data being returned from a POST/UPDATE/PUT/DELETE call.
How do I do this now in my app considering I'm using sveltekit the SSR way; so basic HTML forms calling endpoint methods, not the FE components calling endpoints asynchronously?
(I know I can handle the errors, but don't see how to display various POST responses)
use case:
I'm using Supabase Auth from an endpoint, so my magic link login page is a simple HTML form that when submitted posts the email to its shadow endpoint.
If sending the link fails, I can throw an error and the page can handle it, but when it succeeds I want to be able to show an alert in the page saying the email has been sent.
Beta Was this translation helpful? Give feedback.
All reactions