1

I wanna bring FormData to Console to check key and value.

var f = new FormData();
f.append('key1', 'value1');

console.log(f);

f = new FormData(); 

console.log(f); // I wanna know: Does f keep the key `key1` and value `value1`?

When I check in console, nowhere I can see the key and the value.

Can you give me some tip?

Tân
  • 1
  • 15
  • 56
  • 102

3 Answers3

1

If you're using firefox you can send an ajax request and check in the Network monitor the parameters of your request

Here is a screenshot :

Here is a screen shot

Or you can easily print_r($_POST) or $_GET if you are using php .

user297904
  • 417
  • 1
  • 4
  • 12
  • Oh. Sorry. Is there another way to do this without making an ajax request? – Tân Dec 30 '15 at 15:31
  • I don't think that this is possible but you should definitly check this question: http://stackoverflow.com/questions/17066875/how-to-inspect-formdata – user297904 Dec 30 '15 at 15:34
  • I am not sure if this will work for you but give it a try, `console.log(JSON.stringify(formdata))` – user297904 Dec 30 '15 at 15:37
  • `console.log(JSON.stringify(formdata))` prints `{}` :) – Tân Dec 30 '15 at 15:40
  • :/ I think there is no other solution than sending an Ajax request, sorry – user297904 Dec 30 '15 at 15:46
  • @user297904 _":/ I think there is no other solution than sending an Ajax request, sorry"_ Another possible approach could be to use Web Worker , `.entries()` , see https://developer.mozilla.org/en-US/docs/Web/API/FormData/entries – guest271314 Dec 30 '15 at 17:08
  • Great ! I have never heard about it, but as I can see from the documentation that is only supported by the last version of Firefox . – user297904 Dec 30 '15 at 17:15
  • 1
    @user297904 _"but as I can see from the documentation that is only supported by the last version of Firefox"_ At chrome, chromium could enable harmony flag , see https://plus.google.com/+PaulIrish/posts/T615Md5JPQG – guest271314 Dec 30 '15 at 17:33
1

An approach utilizing Web Workers API , FormData.entries() ; see also Using Web Workers

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>MDN Example - Embedded worker</title>
<script type="text/js-worker">
  // This script WON'T be parsed by JS engines 
  // because its mime-type is text/js-worker.
  // var myVar = "Hello World!";
  var key = "key1";
  var value = "value1";
  // myVar.append('key1', 'value1');
  // Rest of your worker code goes here.
</script>
<script type="text/javascript">
  // This script WILL be parsed by JS engines 
  // because its mime-type is text/javascript.
  function pageLog (sMsg) {
    // Use a fragment: browser will only render/reflow once.
    var oFragm = document.createDocumentFragment();
    oFragm.appendChild(document.createTextNode(sMsg));
    oFragm.appendChild(document.createElement("br"));
    document.querySelector("#logDisplay").appendChild(oFragm);
  }
</script>
<script type="text/js-worker">
  // This script WON'T be parsed by JS engines 
  // because its mime-type is text/js-worker.
  onmessage = function (oEvent) {
    var f = new FormData();
    f.append(key, value);
    for(var pair of f.entries()) {
      postMessage(pair[0] + ", " + pair[1]); 
    }

  };
  // Rest of your worker code goes here.
</script>
<script type="text/javascript">
  // This script WILL be parsed by JS engines 
  // because its mime-type is text/javascript.

  // In the past...:
  // blob builder existed
  // ...but now we use Blob...:
  var blob = new Blob(
               Array.prototype
               .map.call(
                 document.querySelectorAll("script[type=\"text\/js-worker\"]")
               , function (oScript) { return oScript.textContent; })
             , {type: "text/javascript"});

  // Creating a new document.worker property 
  // containing all our "text/js-worker" scripts.
  document.worker = new Worker(window.URL.createObjectURL(blob));

  document.worker.onmessage = function (oEvent) {
    pageLog("Received: " + oEvent.data);
  };

  // Start the worker.
  window.onload = function() { document.worker.postMessage(""); };
</script>
</head>
<body><div id="logDisplay"></div></body>
</html>
guest271314
  • 1
  • 15
  • 104
  • 177
  • Great. +1 for `FormData.entries()` which works only on FF dev edition now. – Tân Dec 30 '15 at 17:26
  • @HappyCoding _"for FormData.entries() which works only on FF dev edition now"_ Should return expected results in chromium , chrome with `--javascript-harmony` flag set , see https://plus.google.com/+PaulIrish/posts/T615Md5JPQG ; `js` at post was tried at chromium 49 – guest271314 Dec 30 '15 at 17:29
  • No. The doc (https://developer.mozilla.org/en-US/docs/Web/API/FormData/entries) says it's only FF 44. Lastest version of FF is 43.0.3. That's is 45.0a2 for FF Dev Edition. I've just tried it. – Tân Dec 30 '15 at 17:34
  • @HappyCoding _"No. The doc (developer.mozilla.org/en-US/docs/Web/API/FormData/entries) says it's only FF 44. Lastest version of FF is 43.0.3. That's is 45.0a2 for FF Dev Edition. I've just tried it."_ Tried at chrome , chromium with `--javascript-harmony` flag set ? Not tried at firefox , here, yet; `js` at post was tried at chromium 49. `FormData.entries()` should be available a firefox nightly – guest271314 Dec 30 '15 at 17:36
  • Sorry, I just go to `chrome://flags/#enable-javascript-harmony` and `Enable Experimental JavaScript`. Then copy example from https://developer.mozilla.org/en-US/docs/Web/API/FormData/entries and try again. Console log gives error message: `formData.entries is not a function` – Tân Dec 30 '15 at 17:43
  • _"I've tried in Chrome with lastest version."_ Results ? Here , launched chromium with `/path/to/chromium --javascript-harmony` – guest271314 Dec 30 '15 at 17:46
  • Same result in Chromium: `formData.entries is not a function` – Tân Dec 30 '15 at 17:59
  • @HappyCoding _"Same result in Chromium: formData.entries is not a function"_ Why is "f" lowercase at `formData.entries` ? – guest271314 Dec 30 '15 at 18:00
  • That's an example on Mozilla dev page. I just only copy it, don't edit anything. Paste to FF Dev -> it's working. Paste to Chrome and Chromium -> get error above (although I enables `--javascript-harmony` on both of Chrome and Chromium) – Tân Dec 30 '15 at 18:05
  • @HappyCoding `html` , `js` at post was tried at `file://` protocol . To enable reading local files at chrome , chromium, may be necessary to also set `--allow-file-access-from-files` flag ; see http://peter.sh/experiments/chromium-command-line-switches/ – guest271314 Dec 30 '15 at 18:15
  • Sorry. I follow http://www.chrome-allow-file-access-from-file.com/ and try again. But it's still not working. I give up now. Thank you so much. – Tân Dec 30 '15 at 18:26
  • @HappyCoding See http://askubuntu.com/questions/160245/how-do-i-make-the-google-chrome-flag-allow-file-access-from-files-permanent/ – guest271314 Dec 31 '15 at 03:40
1
// Log entries of a FormData object to the console as an object.
export const logFormData = (formData) => {
    const entries = formData.entries();
    const result = {};
    let next;
    let pair;
    while ((next = entries.next()) && next.done === false) {
        pair = next.value;
        result[pair[0]] = pair[1];
    }
    console.log(result);
};

MDN doc on .entries()

MDN doc on .next() and .done

2540625
  • 11,022
  • 8
  • 52
  • 58