【问题标题】:"400 bad request" when posting data to asp.net core web api with FormData in XmlHttpRequest使用 XmlHttpRequest 中的 FormData 将数据发布到 asp.net 核心 Web api 时出现“400 错误请求”
【发布时间】:2019-10-08 13:25:51
【问题描述】:

我有一个带有此操作的 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; }
    }

我从 javascript SPA 调用操作,使用 XHR 如下:

    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 : bad request”响应。
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 有什么问题呢?

【问题讨论】:

  • 您登录uridata 以确认他们是您认为的那样吗?
  • 是的,uri 是正确的,数据日志显示在我的帖子末尾

标签: javascript xmlhttprequest asp.net-core-webapi form-data


【解决方案1】:

如果您想使用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 日期: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

【讨论】:

  • 它起作用了,我现在更好地理解了 FormData 是如何工作的。非常感谢邹星!
猜你喜欢
  • 2020-03-24
  • 1970-01-01
  • 2021-11-22
  • 2018-02-01
  • 2019-05-03
  • 2021-01-07
  • 2020-06-30
  • 2021-01-21
  • 1970-01-01
相关资源
最近更新 更多