Search code examples
javascriptsveltekit

How to redirect after submitting form


I'm new to Javascript and Svelte and I'm trying to redirect after submitting a form. I've tried many things, but the goto function seems to be the only thing that even tries to load the redirected page. Unfortunately I get the following error message. Why does it happen?

500
Unexpected token < in JSON at position 0

Here is my code:

<script>
    import { goto } from '$app/navigation';

    let title;
    let description;
    let images;

    async function submitForm() {
        const dataArray = new FormData();
        dataArray.append("title", title);
        dataArray.append("description", description);
        dataArray.append("image", images[0]);

        const submit = await fetch("new.json", {
            method: "post",
            body: dataArray
        })

        const data = await submit.json();
        // do something with data

        goto("/");
    }
</script>

Solution

  • use window.location.href = "http://stackoverflow.com"; to get redirect

    window.location.href = "http://stackoverflow.com";