问题描述
我有一个文本区域:
<textarea cols="20" id="testtextbox" name="testtextbox" rows="2">test</textarea>
我通过构建一个 FormData 对象来发布它:
and I POST it by building a FormData object:
var newForm = $('<form></form>').append($("#testtextbox"))
var formdata = new FormData(newForm.get(0));
var xhr = new XMLHttpRequest();
xhr.open('POST', '/', true);
xhr.send(formdata);
我希望这会发布该文本区域的值,该文本区域适用于 Chrome 和 Firefox.但是,在 Edge 42.17134 上,发布的请求正文是:
I would expect this to post the value of that one textarea, which works on Chrome and Firefox.However, on Edge 42.17134 the POSTed request body is:
-----------------------------7e2203930476
Content-Disposition: form-data; name="testtextbox"
-----------------------------7e2203930476--
这在早期版本的 Edge 中也能正常工作.难道我做错了什么?据我所知,我不依赖任何已弃用的功能.
This also works fine in previous versions of Edge.Am I doing something wrong? As far as I can tell I'm not relying on any deprecated features.
推荐答案
根据你的描述和代码,建议你可以查看formdata 的官方 API 并按照以下两种方式修改您的代码.
According to your description and code, I suggest that you could check the official API about formdata and modify your code like the following two ways.
1.使用 formdata.append 发布值
1.use formdata.append to post the value
<textarea cols="20" id="testtextbox" name="testtextbox" rows="2">test</textarea>
<script type="text/javascript">
var formdata = new FormData();
formdata.append("testtextbox", testtextbox.value);
var xhr = new XMLHttpRequest();
xhr.open('POST', '/', true);
xhr.send(formdata);
</script>
结果:第一种方式
2.在页面正文中添加表单
2.add a form to the page body
<form id="form1" name="form1">
<textarea cols="20" id="testtextbox" name="testtextbox" rows="2">test</textarea>
</form>
<script type="text/javascript">
$(function () {
var newForm = $("#form1");
var formdata = new FormData(newForm.get(0));
var xhr = new XMLHttpRequest();
xhr.open('POST', '/', true);
xhr.send(formdata);
})
</script>
结果:第二种方式
此致,
珍妮弗
这篇关于FormData 构造函数在 Edge 中丢失 textarea 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!