问题描述
我正在Express服务器上使用Node'form-data'模块来构造对来自客户端浏览器的提取请求的多部分/表单数据响应(这也是使用fetch的多部分,但在服务器上可以正常接收在服务器上使用multer)。当服务器发回表单数据响应时,在收到响应时,获取客户端出现错误-“无法将内容解析为FormData。”(本机FormData)(注意:这与不解析多部分的Express解析器不同,这是浏览器上的本地获取客户端未解析节点表单数据)服务器响应或客户端响应处理中我在做什么错?
在服务器上
const formdata = require('form-data')
app.post(req,res,next) {
// ... process the request and construct form-data response...//
var form = new formdata();
form.append("serverResponse","Reply from server to fetch request from client")
res.end(form.getBuffer())
}
在客户处
//... send request to server which has no problem,but returncannot decode the response as FormData
return fetch( pRequest )
.then(response => {
return response.formData() //***this throws 'Could not parse content as FormData***
}
.then(result => console.log(JSON.stringify(result))
解决方法
将响应的标题“Content-Type”设置为“multipart/form-data”。