0

I'm trying to send my formdata object to a service worker before submitting the form by using the postMessage() method.

I've tried multiple solutions but I always either get the cant be cloned error or empty form data.

Some of the things I've tried:

var formData = new FormData();
formData.append('photo', image);
formData.append('description', description);

// attempt 1
var msg = {
    form_data: formData,
}

// attempt 2
var msg = {
    form_data: JSON.stringify(formData),
}

// attempt 3
var msg = {
    form_data: JSON.stringify(formData.entries()),
}

// attempt 4

console.log(msg); // form data is either empty or I get an error depending on the attempt
swRegistration.active.postMessage(msg);
failedCoder
  • 1,346
  • 1
  • 14
  • 38
  • Possible duplicate of [How to convert FormData(HTML5 Object) to JSON](https://stackoverflow.com/questions/41431322/how-to-convert-formdatahtml5-object-to-json) – 04FS Oct 29 '19 at 13:46
  • 1
    Interesting... Firefox allows the cloning of the FormData object through postMessage. – Kaiido Oct 29 '19 at 14:17

1 Answers1

0

This worked:

    var object = {};
    formData.forEach((value, key) => {object[key] = value});
    var json = JSON.stringify(object);

    var msg = {
       form_data: json,
    }
failedCoder
  • 1,346
  • 1
  • 14
  • 38