javascript - 获取 POST ReactJs 到 Controller .Net Core

标签 javascript c# node.js reactjs asp.net-core

我在 Fetch POST to controller 中遇到问题,显示错误如 {"errors":{"":["Unexpected character encountered while parsing number: W. Path '', line 1, position 6."]},"title":"出现一个或多个验证错误。","status":400,"traceId":"0HLOGKVEBUTKL:00000007"}。当我也调试时,在我的 Controller 中它没有传递给 Controller ​​。

这是我发布到的内容

enter image description here enter image description here

我在客户端使用 ReactJs,在服务器端使用 Net Core。这是我的代码片段:

添加列表.js

 fetch('api/SampleData/AddEmployee', {
   method: 'POST',
   headers: {
     'Accept': 'application/json; charset=utf-8',
     'Content-Type': 'application/json;charset=UTF-8'
   },
   body: data,
 }).then((response) => response.json())
 .then((responseJson) => {
   console.log('Success:', JSON.stringify(responseJson));
   this.props.history.push("/list-data");
 })
   .catch(error => console.error('Error:', error));

示例数据 Controller .cs

[HttpPost("[action]")]
public JsonResult AddEmployee(EmployeesViewModel employee)
{
}

starrup.cs

 app.UseMvc(routes =>
 {
   routes.MapRoute(
     name: "default",
     template: "{controller}/{action=Index}/{id?}");

   routes.MapRoute(
     name: "api",
     template: "api/{controller}/{action}/{id?}");
 });

最佳答案

你不需要这个配置

   routes.MapRoute(
     name: "api",
     template: "api/{controller}/{action}/{id?}");

而是像这样在你的 Controller 中使用路由

[Route("api/[controller]/[action]")]
public class YourController : Controller

关于javascript - 获取 POST ReactJs 到 Controller .Net Core,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57193853/

相关文章:

javascript - 我如何使用 jquery 删除 "Read more"?

javascript - jquery "$.when"函数未按预期工作

c# - WPF 组合框所选项目错误 - 显示 "System.Data.Entity.DynamicProxies"

c# - 添加迁移时无法加载文件或程序集网络标准

node.js - Node.js 中的 Node 调度回调函数

node.js - 使用 TCP 负载平衡器代理 WebSockets 没有粘性 session

Javascript三.js平滑相机运动

javascript - Attr disable 不会禁用我的按钮

c# - VS Express和专业有冲突吗?

javascript - React 服务器端渲染在前端重新渲染?