javascript new FormData() "application/x-www-form-urlencoded"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7542586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
new FormData() "application/x-www-form-urlencoded"
提问by Gert Cuykens
Couchdb only parse application/x-www-form-urlencoded. Is there a FormData() attribute that set the enctype?
Couchdb 只解析 application/x-www-form-urlencoded。是否有设置 enctype 的 FormData() 属性?
xhr.open('put',document.myForm.action,false)
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
xhr.send(new FormData(document.myForm))
采纳答案by natevw
No, the XHR2 "send" methodis specified to always send FormData objects as multipart/form-data.
不,XHR2“发送”方法被指定为总是将 FormData 对象作为 multipart/form-data 发送。
As ampersand suggests, one option would be to use the jquery.couch.jsplugin that's built into every CouchDB instance within Futon.
正如&符号所暗示的那样,一种选择是使用内置于 Futon 中每个 CouchDB 实例中的jquery.couch.js插件。
If you like a bit more generic HTTP interface, Fermataalso has support for URL encoded requests:
如果您喜欢更通用的 HTTP 接口,Fermata还支持 URL 编码请求:
fermata.json(document.myForm.action).put({'Content-Type':"application/x-www-form-urlencoded"}, {...form fields...});
Another option would be to send JSON to your update function (which I'm assuming is the 'action' URL of your form) instead.
另一种选择是将 JSON 发送到您的更新函数(我假设它是表单的“操作”URL)。
Of course, the trick with any of these is that you'll have to extract the form fields yourself, since there's no easy DOM-level equivalent of new FormData(document.myForm)
that returns an Object instead AFAIK.
当然,其中任何一个的技巧是您必须自己提取表单字段,因为没有简单的 DOM 级等效项new FormData(document.myForm)
返回一个对象而不是 AFAIK。
回答by cuixiping
FormData will always be sent as multipart/form-data
.
FormData 将始终作为multipart/form-data
.
If you want to send FormData as x-www-form-urlencoded
, encode the content items:
如果要将 FormData 发送为x-www-form-urlencoded
,请对内容项进行编码:
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 URLSearchParamslike this:
你也可以像这样使用URLSearchParams:
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 old browsers which doesn't support URLSearchParamsAPI, you can use one of polyfills:
对于不支持URLSearchParamsAPI 的旧浏览器,您可以使用 polyfills 之一:
回答by StanE
Some time ago I wrote the following function. It collects form values and encodes them url encoded, so they can be sent with content type application/x-www-form-urlencoded
:
前段时间我写了以下函数。它收集表单值并将它们编码为 url 编码,因此它们可以与内容类型一起发送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);