Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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&quot;; to get redirect

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading