0

您好,我使用 FormData WebApi 来收集 Formdata 并通过 Ajax 将其发送到我的 Ruby on Rails 后端。一切都按预期工作,但我在 IE11 中遇到问题。

问题: * 如果我编辑表单并且没有名称并选中“男性”单选按钮,则请求被破坏*即使我不编辑表单,即发送它为空。

标记示例:

<form action="/">

  <label for="input-1">Name</label>
  <input type="text" name="input-1" value="">

  <label>
    <input type="radio" name="radio-1" value="male"/>
    <span>Male</span>
  </label>

  <label>
    <input type="radio" name="radio-1" value="female"/>
    <span>Female</span>
  </label>

  <button name="button" type="submit">submit</button>
</form>

*请求数据

-----------------------------7e33b51410242
Content-Disposition: form-data; name="radio-1[sex]"

male
-----------------------------7e33b51410242
Content-Disposition: form-data; input-1="
-----------------------------7e33b51410242--
4

1 回答 1

0

尝试如下修改您的代码:

<form action="/">

  <label for="input_1">Name</label>
  <input type="text" name="input_1" value="">

  <label>
    <input type="radio" name="radio_1" value="male"/>
    <span>Male</span>
  </label>

  <label>
    <input type="radio" name="radio_1" value="female"/>
    <span>Female</span>
  </label>

  <button name="button" type="submit">submit</button>
</form>

此外,如果仍然无法正常工作,您可以创建一个 FormData 对象并发送值,代码如下:

var data = new FormData();
//you could use JavaScript script to get the value from the input elements.
data.append("filesToDelete", "Value");
data.append("clientContactId", 
(clientContactId != undefined || clientContactId != null) ? clientContactId : ''));

$.ajax({
        type: "POST",
        url: "/api/FileAttachment/UploadFiles",
        /* ONLY IF YOU ARE UPLOADING A FILE
        contentType: false,
        processData: false, */
        dataType: "JSON"
        data: data,
        success: function (result) {
        },
        error: function (xhr, status, p3, p4) {
        }
    });

更多信息,请参考以下文章:

使用 FormData 对象FormData.append()

于 2019-09-10T09:50:23.723 回答