[英]FormData change from multipart/form-data to form-urlencoded?
I am using the current javascript to post form data 我正在使用当前的JavaScript发布表单数据
var request = new XMLHttpRequest();
request.open("POST", "/validate",false);
request.send(new FormData(form)); // form is document.getElementById("#form")
With an expressjs
backend using body-parser
with following settings 使用带有
body-parser
的expressjs
后端并进行以下设置
app.use(parser.urlencoded({ extended: false }));
The form data is being posted properly with content-type
to multipart/form-data;
表单数据已正确地以
content-type
到multipart/form-data;
but according to body-parser they don't parse multipart content. 但根据body-parser的分析,它们不会解析多部分内容。 How can i change the form submission to either
urlencoded
or json
both of which can be parsed by the backend ? 我如何将表单提交更改为
urlencoded
或json
都可以由后端解析?
Try add a header to request and convert data to url-encode format 尝试添加标题以请求并将数据转换为url编码格式
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 request = new XMLHttpRequest();
request.open('POST', '/validate', false);
request.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
request.send(urlencodeFormData(new FormData(form)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.