我已经编写了一个React-component,它应该用于我应用程序中的所有表单。当单击某个按钮时,我会进行一些验证,最后我想将表单发送到服务器。这就是这部分当前的样子:
// get what should be submitted
const formData = new FormData(theForm)); // theForm is the DOM-element
// post the form-data element
fetch(theForm.action,
{
credentials: "include", //pass cookies, for authentication
method: theForm.method,
headers: {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
//"Content-Type": "application/x-www-form-urlencoded"
},
body: formData
})
.then(res => console.dir(res))
.catch(reason => console.error(reason));
所显示的代码片段在Chrome
中工作正常。然而,在IE11
中并非如此。另一方面,当不注释Content-Type
标头时,它也会在Chrome
中中断。
当发现https://stackoverflow.com/a/46642899/615288时,它总是"multipart/form-data"
。但是,即使我将其设置为multipart/form-data
,这些值也不会发送到服务器。
我使用的是来自https://github.com/jimmywarting/FormData和whatwg-fetch
的FormData polyfill。
我看不出这里发生了什么,因为从版本9开始,FormData应该可以在IE中工作。
旁注:当注释掉整个头部分时,它仍然在Chrome
中工作,因为浏览器似乎可以猜测正确的部分(就像在开发人员工具中看到的那样)。
发布于 2017-12-04 00:53:18
今天有人在回收站向我报告了这件事。
https://github.com/jimmywarting/FormData/issues/44
显然,"IE无法在正文是类型化Blob的XHR上设置Content-Type头“,这就是为什么您会得到错误的content-type头。将版本更新到3.0.7可能会修复此问题
发布于 2018-10-08 23:04:49
我遇到过这个问题,但在经历了许多挫折之后,我发现向FormData
对象追加一个额外的字段会使所有字段突然出现在服务器上。
const formData = new FormData(theForm);
// this somehow makes it work
formData.append('fakefield', 'nothing to see here!');
fetch(theForm.action).then(/* do the stuff */);
我不知道它为什么工作,但是在我添加这行代码之前,服务器接收{}
作为请求主体,然后它接收{ myformfield: 'myvalue', myotherfield: 'myothervalue', fakefield: 'nothing to see here!' }
希望这能为某些人节省一点时间:)
https://stackoverflow.com/questions/47593227
复制相似问题