Closed maskletter closed 5 years ago
const form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
if(err){
console.log(err)
next();
return;
}
next(fields);
});
//html
var xml = new XMLHttpRequest();
xml.open('post', '/bef/upload?f=123')
xml.onload = function(){
console.log(xml.responseText)
}
var json = {
name: 'tom'
}
xml.send(JSON.stringify(json))
@maskletter, thanks for reporting.
As I understood your "content-type" is "text/plain;charset=UTF-8", right? In that case no need to use Formidable, Express/NodeJS already can parse it. (Formidable is mainly for handling file uploads with "content-type" equal to "multipart/form-data").
If I'm mistaken, please correct me. Waiting for your response, to decide whether we need to investigate or close the issue.
return new Promise((resolve, reject) => {
console.log('request')
const form = new formidable.IncomingForm();
form.onPart = function(part) {
if (!part.filename) {
form.handlePart(part);
}
}
form.parse(request, function(err, fields, files) {
if(err){
throw err
}
console.log('aaaaaaaaaaaa')
resolve(fields);
});
})
var xml = new XMLHttpRequest();
xml.open('post', '/user/login2?f=123')
xml.onload = function(){
console.log(xml.responseText)
}
var json = {
name: 'tom'
}
xml.send()
This code will output “aaaaaaaaaaaa”
var xml = new XMLHttpRequest();
xml.open('post', '/user/login2?f=123')
xml.onload = function(){
console.log(xml.responseText)
}
var json = {
name: 'tom'
}
xml.send(JSON.stringify(json))
This code will not have any output.
Is because formidable can't parse JSON.stringify(json), causing no return?
@maskletter, Yes. JSON.stringify()
will return a string.
Then xml.send(String)
will try to automatically set the Content-Type
header, perhaps to undefined
or to text/plain
(unsure of it).
But surely it will not set content type to multipart/form-data
or something that is parseable for Formidable. Thus Formidable will ignore it, and what you send will get into req.body
"as is".
@xarguments would it be possible to have a fallback for the text/plain;charset=UTF-8
data type implemented then? Since no intervention is needed on the part of formidable
, you could just pass the data directly through to express
as you suggested, thus preventing this from erroring out on plain text input.
This individual had the same error that I am having now, where even when uploading a non-text file like a photo the fields
and files
objects are empty and we get the error that this issue's OP experienced.
Can't receive such data?