How to inspect FormData?

前端 未结 15 1333
悲&欢浪女
悲&欢浪女 2020-11-22 08:23

I\'ve tried console.log and looping through it using for in.

Here it the MDN Reference on FormData.

Both attempts are in this fi

相关标签:
15条回答
  • 2020-11-22 08:47

    In certain cases, the use of :

    for(var pair of formData.entries(){... 
    

    is impossible.

    I've used this code in replacement :

    var outputLog = {}, iterator = myFormData.entries(), end = false;
    while(end == false) {
       var item = iterator.next();
       if(item.value!=undefined) {
           outputLog[item.value[0]] = item.value[1];
       } else if(item.done==true) {
           end = true;
       }
        }
    console.log(outputLog);
    

    It's not a very smart code, but it works...

    Hope it's help.

    0 讨论(0)
  • 2020-11-22 08:47

    You have to understand that FormData::entries() returns an instance of Iterator.

    Take this example form:

    <form name="test" id="form-id">
        <label for="name">Name</label>
        <input name="name" id="name" type="text">
        <label for="pass">Password</label>
        <input name="pass" id="pass" type="text">
    </form>
    

    and this JS-loop:

    <script>
        var it = new FormData( document.getElementById('form-id') ).entries();
        var current = {};
        while ( ! current.done ) {
            current = it.next();
            console.info( current )
        }
    </script>
    
    0 讨论(0)
  • 2020-11-22 08:48

    ES6+ solutions:

    To see the structure of form data:

    console.log([...formData])
    

    To see each key-value pair:

    for (let [key, value] of formData.entries()) {
      console.log(`${key}: ${value}`);
    }
    
    0 讨论(0)
提交回复
热议问题