Using JavaScript to send data to the database and not using the default <form>'s submit

I’m using JavaScript to send data to the database and not using the default <form>'s submit.

let g_submitFormData = () =>
{
    const request = new Request(
        BASE_URL + "api/add",
        { headers: {'X-CSRFToken': csrftoken} }
    );

    return fetch(request, {
        method: 'POST',
        mode: 'same-origin',
        body: JSON.stringify(Alpine.store('global_data'))
    })
    .then(response => response.json())
    .then(response =>
    {
        data = response; console.log(data);
        return data;
    });
}

But now I am in a fix when I want to try sending in files as Input.

<input type="file" multiple x-model="$store.global_data.attachments" />

let data = new FormData();
data.append('file',document.querySelector('input[type="file"]').files[0]);
//append other bits of data here. I dunno what Alpine.store returns.
....
body: data
2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.