“在XmlHttpRequest中使用FormData将数据发布到asp.net核心Web api时出现“ 400错误请求”

问题描述 投票:0回答:1

我有一个执行此操作的ASP.Net Core Web API:

    // POST api/TodoList
    [HttpPost]
    public void Post([FromBody] Tache tache)
    {
       TodoListContext.AjouterTache(tache);
    }

这是Tache实体的代码:

    public class Tache
    {
       public int Id { get; set; }

       [DataType(DataType.MultilineText)]
       [Required, MaxLength(250)]
       public string Description { get; set; }

       [DataType(DataType.Date)]
       public DateTime DateCreation { get; set; }

       [DataType(DataType.Date)]
       public DateTime DateEcheance { get; set; }

       public int Priorite { get; set; }

       public bool Terminee { get; set; }
    }

我使用XHR这样的javascript SPA调用动作:

    function ajouterTache() {
       // Construit un objet Tâche à partir des valeurs saisies
       let tache = {}; 
       tache.id = document.getElementById("idTache").value;
       tache.dateCreation = document.getElementById("dateCreation").value;
       tache.dateEcheance = document.getElementById("dateEcheance").value;
       tache.priorite = document.getElementById("priorite").value;
       tache.terminee = document.getElementById("terminee").checked;
       tache.description = document.getElementById("description").value; 
       console.log(tache);

       // Envoie la tâche à l'API
       const req = new XMLHttpRequest();
       req.onload = function () {
          ...
       };

       req.open('POST', uri, true);
       req.setRequestHeader("Content-Type", "application/json");
       req.send(JSON.stringify(tache));
    }

上面的代码可以正常工作,但是这个代码不行:

    function ajouterTache() {
       data = new FormData(document.getElementById("formEdit"));

       // Envoie la tâche à l'API
       const req = new XMLHttpRequest();
       req.onload = function () {
        ...
       };

       req.open('POST', uri, true);
       req.setRequestHeader("Content-Type", "application/json");
       req.send(data);
    }

表单的每个字段都具有正确的名称,已启用并包含有效的输入。

但是我总是得到“ 400:错误请求”响应。Firefox的调试工具在XHR结果中显示以下错误:

输入字符串'----------------------------- 18691991225667'不是有效数字。路径'',第1行,位置43。title:发生一个或多个验证错误

我在发送xhr来观看数据对象的内容之前添加了此代码:

    let formData = new FormData(document.getElementById("formEdit"));
    for (let pair of formData.entries()) {
       console.log(pair[0] + ': ' + pair[1] + ", " + typeof pair[1]);
    }

这里是结果:

idTache: 11
dateCreation: 2019-10-08
dateEcheance: 2019-10-22
priorite: 1
terminee: on
description: essai

似乎是正确的。那我使用FormData有什么问题呢?

javascript xmlhttprequest asp.net-core-webapi form-data
1个回答
0
投票

如果要使用FormData发布数据,则不应将Content-Type设置为application/json。此外,请在操作参数上使用[FromForm]而不是[FromBody]

1。在js代码中删除以下行

req.setRequestHeader("Content-Type", "application/json");

2。使用[FromForm]

[HttpPost]
 public void Post([FromForm] Tache tache)

这里是结果:

idTache:11日期创建:2019-10-08日期Echeance:2019-10-22优先级:1终端:在说明:essai

由于收到的数据是模型类型Tache,因此需要传递名为id的属性,而不是记录结果中显示的idTache

您不显示您的查看代码,我建议您在输入文本框中使用name="id"

在我的情况下,正确的日志结果为__RequestVerificationToken:,如果您使用<form id="formEdit">,也会显示出来:

id: 11
dateCreation: 2019-10-08
dateEcheance: 2019-10-22
priorite: 1
terminee: on
description: essai
__RequestVerificationToken:xxxxxxxx
© www.soinside.com 2019 - 2024. All rights reserved.