0

Trying to send form data in JSON format but IE does not support formData.forEach

 var form = document.querySelector('#Form');
             var formData = new FormData(form);
             var object = {};
             formData.forEach(function(value, key){
                 object[key] = value;
             });
             var json = JSON.stringify(object);
             xhr.send(json);

How can I convert that forEach in for loop? Also, I've already added the polyfill so it's not that

Dortimer
  • 619
  • 8
  • 25
UserEsp
  • 415
  • 1
  • 7
  • 29
  • If you don't mind using libraries, UnderscoreJS has a lot of pretty useful functions (including `forEach` and many others). https://underscorejs.org/ – Dortimer Dec 11 '19 at 20:48
  • use `formData.getAll().forEach(....` – Eldar Dec 11 '19 at 20:48
  • 1
    Seems to be answered [here](https://stackoverflow.com/questions/16813469/javascript-method-foreach-not-supported-from-internet-explorer). –  Dec 11 '19 at 20:48
  • 1
    Related: https://stackoverflow.com/questions/22195065/how-to-send-a-json-object-using-html-form-data/22195193#22195193 – Taplar Dec 11 '19 at 20:49
  • `FormData` does not have a `forEach` method. You can turn it into an array using `formData.getAll()`, at which point the polyfill should allow you to use `.forEach()` on the resulting array. – Tyler Roper Dec 11 '19 at 20:49

1 Answers1

0

After reading the comments and links that people provided me, this worked for me.

var object = {};
 var dataArray=($("#form").serializeArray());
    for (var i in dataArray) {
         object[dataArray[i].name] = dataArray[i].value;
    }
 var json = JSON.stringify(object);
UserEsp
  • 415
  • 1
  • 7
  • 29