0

I'm making a Form purely in JS using a FormData Object (so without a real HTML Form) that I want to submit with POST method to another URL with a redirect (in the same way as I compile a form and press the Submit button getting redirected to the action URL).

I've tried with this code:

function sendFileToDetailsStep(fileToUpload) {
            var formData = new FormData();
            formData.append("file", fileToUpload);
            formData.append("csrfmiddlewaretoken", "{{ csrf_token() }}");
            formData.set("method", "POST");
            formData.set("action", "{{ route('quotation.details') }}");
            formData.submit();
        }

But I get a error "Uncaught (in promise) TypeError: formData.submit is not a function", where I'm wrong?

1 Answers1

-1

submit() is a method available on forms; FormData objects are not forms.

A FormData object is designed to collect data (and can be initialized by passing it a form object) to be passed to fetch or XMLHttpRequest for making Ajax requests. It doesn't have any built-in capabilities to make an HTTP request or to trigger navigation to a new page.

You could use Ajax to make the request, but that wouldn't handle navigating to a new page.

It would be easiest to use a real <form> for this.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I can't because I'm handling the drop of a file in the browser window and JS doesn't allow to populate a file type input. – Mattia Del Franco Feb 12 '21 at 16:28
  • @MattiaDelFranco — Yes, it does: https://stackoverflow.com/questions/8006715/drag-drop-files-into-standard-html-file-input – Quentin Feb 12 '21 at 16:30