How to inspect FormData?

前端 未结 15 1332
悲&欢浪女
悲&欢浪女 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:34

    The MDN suggests the following form:

    let formData = new FormData();
    formData.append('name', 'Alex Johnson')
    for(let keyValuePair of formData.entries()){
        console.log(keyValuePair); //has form ['name','Alex Johnson']
    }
    

    Alternatively

    for (let [key, value] of formData.entries()) {
     console.log(key, ':', value);
    }
    

    Consider adding ES+ Polyfills, in case the browser or environment doesn't support latest JavaScript and FormData API.

    I hope this helps.

    0 讨论(0)
  • 2020-11-22 08:35
      function abc(){ 
        var form = $('#form_name')[0]; 
            var formData = new FormData(form);
            for (var [key, value] of formData.entries()) { 
                console.log(key, value);
            }
            $.ajax({
                type: "POST",
                url: " ",
                data:  formData,
                contentType: false,
                cache: false,
                processData:false,
                beforeSend: function() {
    
                },
                success: function(data) {
    
    
                },
    
           });
    }
    
    0 讨论(0)
  • 2020-11-22 08:44

    Few short answers

    [...fd] // shortest devtool solution
    console.log(...fd) // shortest script solution
    console.log([...fd]) // Think 2D array makes it more readable
    console.log(Object.fromEntries(fd)) // Works if all fields are uniq
    new Response(fd).text().then(console.log) // To see the entire raw body
    

    Longer answer

    Some of wish suggest logging each entry of entries, but the console.log can also take multiple arguments
    console.log(foo, bar)
    To take any number of argument you could use the apply method and call it as such: console.log.apply(console, array).
    But there is a new ES6 way to apply arguments with spread operator and iterator
    console.log(...array).

    Knowing this, And the fact that FormData and both array's has a Symbol.iterator method in it's prototype you could just simply do this without having to loop over it, or calling .entries() to get the hold of iterator

    var fd = new FormData()
    
    fd.append('key1', 'value1')
    fd.append('key2', 'value2')
    
    console.log(...fd)

    If you would like to inspect what the raw body would look like then you could use the Response constructor (part of fetch API), this will convert you formdata to what it would actually look like when you upload the formdata

    var fd = new FormData()
    
    fd.append('key1', 'value1')
    fd.append('key2', 'value2')
    
    new Response(fd).text().then(console.log)

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

    Updated Method:

    As of March 2016, recent versions of Chrome and Firefox now support using FormData.entries() to inspect FormData. Source.

    // Create a test FormData object
    var formData = new FormData();
    formData.append('key1', 'value1');
    formData.append('key2', 'value2');
    
    // Display the key/value pairs
    for (var pair of formData.entries()) {
        console.log(pair[0]+ ', ' + pair[1]); 
    }
    

    Thanks to Ghost Echo and rloth for pointing this out!

    Old Answer:

    After looking at these Mozilla articles, it looks like there is no way to get data out of a FormData object. You can only use them for building FormData to send via an AJAX request.

    I also just found this question that states the same thing: FormData.append("key", "value") is not working.

    One way around this would be to build up a regular dictionary and then convert it to FormData:

    var myFormData = {
        key1: 300,
        key2: 'hello world'
    };
    
    var fd = new FormData();
    for (var key in myFormData) {
        console.log(key, myFormData[key]);
        fd.append(key, myFormData[key]);
    }
    

    If you want to debug a plain FormData object, you could also send it in order to examine it in the network request console:

    var xhr = new XMLHttpRequest;
    xhr.open('POST', '/', true);
    xhr.send(fd);
    
    0 讨论(0)
  • 2020-11-22 08:45

    Already answered but if you want to retrieve values in an easy way from a submitted form you can use the spread operator combined with creating a new Map iterable to get a nice structure.

    new Map([...new FormData(form)])

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

    When I am working on Angular 5+ (with TypeScript 2.4.2), I tried as follows and it works except a static checking error but also for(var pair of formData.entries()) is not working.

    formData.forEach((value,key) => {
          console.log(key+" "+value)
    });
    

    var formData = new FormData();
    formData.append('key1', 'value1');
    formData.append('key2', 'value2');
    
    formData.forEach((value,key) => {
      console.log(key+" "+value)
    });

    Check at Stackblitz

    0 讨论(0)
提交回复
热议问题