post与.net core接受请求参数

///ffff
  const res = await instancs.post(
    "Know/DocList_Insert",
    {
      Id: 1,
      Name: "酷酷酷酷酷",
      ClientId: 1,
      EqId: 1,
    },
    { headers: { "Content-Type": "application/form-data" } }
  );
  console.log("res:", res);
};
后端

[HttpPost("DocList_Insert")]
public String DocList_Insert()
{
Debug.WriteLine("基础文档查找");
using (var ctx = new MyDbContext())
{
var knowType = new KnowType {Type="doc", Name = "2", ClientId= 1, EqId= 1 };
ctx.KnowType.Add(knowType);
ctx.SaveChanges();
return "200";//return 所有数据
}
}

修改后:

[HttpPost("DocList_Insert")]
public String DocList_Insert([FromForm] dataForm data)
{
Debug.WriteLine("基础文档查找");
using (var ctx = new MyDbContext())
{
Debug.WriteLine("插入数据:{0}",data);
var knowType = new KnowType {Type= data.Type, Name = data.Name, ClientId= data.ClientId, EqId= data.EqId };
ctx.KnowType.Add(knowType);
ctx.SaveChanges();
return "200";//return 所有数据
}
}
public class dataForm
{
public int ClientId { get; set; }
public int EqId { get; set; }
public string Name { get; set; }
public string Type { get; set; }
}

报错400原因是前端的请求头写错了
错误:{ headers: { "Content-Type": "application/form-data" } }

正确:{ headers: { "Content-Type": "multipart/form-data" } }

 

posted @ 2024-05-11 13:29  爱晒太阳的懒猫。。  阅读(14)  评论(0编辑  收藏  举报