最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Fetch POST ReactJs to Controller .Net Core - Stack Overflow

programmeradmin8浏览0评论

I have problem in Fetch POST to controller, an showing error like {"errors":{"":["Unexpected character encountered while parsing number: W. Path '', line 1, position 6."]},"title":"One or more validation errors occurred.","status":400,"traceId":"0HLOGKVEBUTKL:00000007"}. When I debbug too, in my controller it's not passing to controller.

This what I POST to

I'm using ReactJs for client-side, and Net Core for server-side. Here my code snippet:

AddList.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));

SampleDataController.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?}");
 });

I have problem in Fetch POST to controller, an showing error like {"errors":{"":["Unexpected character encountered while parsing number: W. Path '', line 1, position 6."]},"title":"One or more validation errors occurred.","status":400,"traceId":"0HLOGKVEBUTKL:00000007"}. When I debbug too, in my controller it's not passing to controller.

This what I POST to

I'm using ReactJs for client-side, and Net Core for server-side. Here my code snippet:

AddList.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));

SampleDataController.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?}");
 });
Share Improve this question edited Oct 10, 2019 at 14:44 Tony 20.2k7 gold badges41 silver badges62 bronze badges asked Jul 25, 2019 at 3:26 StfvnsStfvns 1,0416 gold badges16 silver badges42 bronze badges 4
  • can you try it without Content-Type. It worked for me when i was dealing with formdata – deathstroke Commented Jul 25, 2019 at 3:49
  • different error {"type":"https://tools.ietf/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"0HLOGKVEBUTKP:00000008"} Unsupported Media Type @mkamranhamid – Stfvns Commented Jul 25, 2019 at 3:55
  • I've no idea about but by seeing your error I guess your target resource should be explicitly told what kind of data it should be receiving in body like this 'Content-Type': 'multipart/form-data' – deathstroke Commented Jul 25, 2019 at 4:02
  • can you try this too – deathstroke Commented Jul 25, 2019 at 4:04
Add a ment  | 

2 Answers 2

Reset to default 3

You dont need this config

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

Instead use routing in your controller like this

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

Instead of

body: data,

can you try this?

body: JSON.stringify(data),
发布评论

评论列表(0)

  1. 暂无评论