Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在post Node.js中获取表单数据中的额外表单字段_Javascript_Html_Node.js_Multipartform Data_Form Data - Fatal编程技术网

Javascript 如何在post Node.js中获取表单数据中的额外表单字段

Javascript 如何在post Node.js中获取表单数据中的额外表单字段,javascript,html,node.js,multipartform-data,form-data,Javascript,Html,Node.js,Multipartform Data,Form Data,像name这样的键值对被附加到客户端XMLHttpRequest中的表单数据中,并与要上载的文件一起发送 如何在使用Node.js的服务器端提取这些值 浏览器: formdata = new FormData(); for( var x = 0;x < files.length;x = x+1){ formdata.append('file',files[x]); } formdata.append('name',name); //name appended here to fo

name
这样的键值对被附加到客户端
XMLHttpRequest
中的表单数据中,并与要上载的文件一起发送

如何在使用Node.js的服务器端提取这些值

浏览器

formdata = new FormData();
for( var x = 0;x < files.length;x = x+1){
    formdata.append('file',files[x]);
}
formdata.append('name',name);  //name appended here to form-data
formdata.append('email',email);
formdata.append('reason',reason);
formdata.append('location',location);
var xhr = new XMLHttpRequest();
var url = "/sign_8081";
xhr.open("POST" ,url);
xhr.withCredentials = true;
router.post('/sign_8081', (req, res) => {
    console.log("sign 8081called"+JSON.stringify(req.body))
    console.log(req.query.name);
    console.log(req.body.name); //want name key value in Node.js code 
    let body = '';
    var rb;
    req.on('data', chunk => {
        body += chunk.toString(); // convert Buffer to string
    });
    req.on('end', () => {      
        rb = parse(body)
        console.log(rb.name);
    });
    console.log(req.file.filename)
    res.send(req.file.filename)                
});

如果您使用的是expressjs,请使用正文解析中间件,如
express.json()
express.urlencoded()
,然后您可以访问表单输入
req.body

   var app = express()

   app.use(express.json()) // for parsing application/json
   app.use(express.urlencoded({ extended: true })) // for parsing application/x-   www-form-urlencoded

    router.post('/sign_8081', (req, res) => {
    console.log("sign 8081called"+JSON.stringify(req.body))
    console.log(req.query.name);// do not use this
    console.log(req.body); // object of inputs
    let body = '';
    var rb;
    req.on('data', chunk => {
        body += chunk.toString(); // convert Buffer to string
    });
    req.on('end', () => {      
        rb = parse(body)
        console.log(rb.name);
    });
    console.log(req.file.filename)
    res.send(req.file.filename)                
});

有关expressjs请求对象的更多信息,我更新了您的帖子并修复了缩进。您能否改进标题,例如“如何使用Node.js从POST中的表单数据中获取额外字段”