Created
January 26, 2024 08:50
-
-
Save indreklasn/9252bb4c8bb644b7546b92578e27b193 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
import { onMount } from 'svelte'; | |
import axios from 'axios'; | |
let csrfToken = ''; | |
onMount(async () => { | |
try { | |
const response = await axios.get('/api/get-csrf-token'); | |
csrfToken = response.data.csrfToken; | |
} catch (error) { | |
console.error('Error fetching CSRF token:', error); | |
} | |
}); | |
async function submitForm() { | |
const formData = new FormData(); | |
formData.append('_csrf', csrfToken); | |
// Append other form data here | |
try { | |
const response = await axios.post('/api/submit-form', formData); | |
// Handle success | |
} catch (error) { | |
// Handle error | |
} | |
} | |
</script> | |
<form on:submit|preventDefault={submitForm}> | |
<input type="hidden" name="_csrf" bind:value={csrfToken} /> | |
<!-- Your form fields go here --> | |
<button type="submit">Submit</button> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment