Why is FormData so secretive / private?

As soon as I create FormData p>

var form = new FormData(); form.append("hash", this.S.Storage.get('h_token')); 

I can’t strengthen it:

 JSON.stringify(form) // returns "{}" 

I can not miss it:

 _.each(form, function(val){ // nope }) 

and I can not register it on the console (without any useful results).

 console.log(form); 

I have yet to come across such a secretive type in JavaScript.

+4
source share
2 answers

It is so discreet that it is intended only for transferring data to an AJAX request and saves you from having to encode your data in "multipart / form-data" on your own, as is done when the form is submitted regularly, so you should use it only at the last moment, when you submit an AJAX request, rather than passing it to other functions.

Do it if you need access to the same data anywhwere else, you can just pass a simple JS object and write a simple helper, for example:

 function createFormData(myData) { // It could also take an existing form for some values from the user var formData = new FormData(/** FormElement form */); for (var key in myData) { formData.append(key, myData[key]); } return formData; } sendMyInfo: function(params) // Add some data to the default params params.hash = this.S.Storage.get('h_token')); params.cacheBuster = (new Date()).valueOf(); // Inspect it console.log(myData); // Wherever you actually need FormData, create it for that purpose only.... var oReq = new XMLHttpRequest(); oReq.open("POST", "stash.php", true); oReq.send(createFormData(myData)); }; 
+3
source

I can’t strengthen it

Because it is a "host object" (provided by the host environment, browser) and host objects cannot be compressed.

I can not get through it

This probably means that it does not have enumerable properties.

and I can not register it on the console (without any useful results).

In fact, console.log shows the only method defined in the specification , .append .

Why are there no enumerated properties ? I don’t know, but considering that you are responsible for filling out FormData, you should know its contents in advance, right?

+3
source

Source: https://habr.com/ru/post/1487157/


All Articles