New FormData () application "application / x-www-form-urlencoded"

Couchdb only parses the application / x -www-form-urlencoded. Is there a FormData () attribute that sets enctype?

xhr.open('put',document.myForm.action,false) xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded') xhr.send(new FormData(document.myForm)) 
+7
source share
3 answers

No, the XHR2 "send" method is specified to always send FormData objects as multipart / form-data.

As the ampersand suggests, one option would be to use the jquery.couch.js plugin built into every instance of CouchDB in Futon.

If you like a more general, general HTTP interface, Fermata also supports URL encoded requests:

 fermata.json(document.myForm.action).put({'Content-Type':"application/x-www-form-urlencoded"}, {...form fields...}); 

Another option is to send JSON to your update function (which I assume is the action URL of your form).

Of course, the trick with any of them is that you have to extract the form fields yourself, since there is no simple equivalent to the DOM level new FormData(document.myForm) , which returns an object instead of AFAIK.

+7
source

FormData will always be sent as multipart/form-data .

If you want to submit FormData as x-www-form-urlencoded , encode content elements:

 function urlencodeFormData(fd){ var s = ''; function encode(s){ return encodeURIComponent(s).replace(/%20/g,'+'); } for(var pair of fd.entries()){ if(typeof pair[1]=='string'){ s += (s?'&':'') + encode(pair[0])+'='+encode(pair[1]); } } return s; } var form = document.myForm; xhr.open('POST', form.action, false); xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded') xhr.send(urlencodeFormData(new FormData(form))); 

you can also use URLSearchParams as follows:

 function urlencodeFormData(fd){ var params = new URLSearchParams(); for(var pair of fd.entries()){ typeof pair[1]=='string' && params.append(pair[0], pair[1]); } return params.toString(); } 

For older browsers that do not support the URLSearchParams API, you can use one of the poly-regiments:

+9
source

Some time ago I wrote the following function. It collects form values ​​and encodes their encoded url, so they can be sent with the content type application/x-www-form-urlencoded :

 function getURLencodedForm(form) { var urlEncode = function(data, rfc3986) { if (typeof rfc3986 === 'undefined') { rfc3986 = true; } // Encode value data = encodeURIComponent(data); data = data.replace(/%20/g, '+'); // RFC 3986 compatibility if (rfc3986) { data = data.replace(/[!'()*]/g, function(c) { return '%' + c.charCodeAt(0).toString(16); }); } return data; }; if (typeof form === 'string') { form = document.getElementById(form); } var url = []; for (var i=0; i < form.elements.length; ++i) { if (form.elements[i].name != '') { url.push(urlEncode(form.elements[i].name) + '=' + urlEncode(form.elements[i].value)); } } return url.join('&'); } // Example (you can test & execute this here on this page on stackoverflow) var url = getURLencodedForm('post-form'); alert(url); 
0
source

All Articles