我想将FormData带到控制台以检查键和值。

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`?


在控制台中签入时,在任何地方都看不到键和值。

你能给我一些小费吗?

最佳答案

一种利用Web Workers APIFormData.entries()的方法;另见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>

关于javascript - 在控制台中检查FormData键和值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34532157/

10-10 00:44