使用来自服务器的表单数据在客户端上获取响应

fetch response on client with form-data from server(使用来自服务器的表单数据在客户端上获取响应)
本文介绍了使用来自服务器的表单数据在客户端上获取响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Express 服务器上使用 Node 'form-data' 模块来构造对来自客户端浏览器的 fetch 请求的 multipart/form-data 响应(这也是使用 fetch 的 multipart,但在服务器上接收良好在服务器上使用 multer).当服务器发回表单数据响应时,我在获取客户端收到响应时收到错误 - 无法将内容解析为 FormData."(本机 FormData)(注意:这与 Express 解析器不解析多部分不同,这是浏览器上的本机获取客户端,不解析节点表单数据)我在服务器响应或客户端响应处理中做错了什么?

I am using the Node 'form-data' module on an Express server to construct a multipart/form-data response to a fetch request from the client browser (which also a multipart using fetch, but is received fine at the server using multer on the server). When server sends back a form-data response I get error at the fetch client on receipt of response - "Could not parse content as FormData."(the native FormData) (Note: This is different from Express parsers not parsing multipart, this is the native fetch client on browser not parsing a node form-data) What am I doing wrong in the server response or client repsonse processing?

在服务器上:

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"的回复.

Set the header "Content-Type" of your response to "multipart/form-data".

这篇关于使用来自服务器的表单数据在客户端上获取响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)