【发布时间】:2019-12-03 06:04:18
【问题描述】:
我在获取 POST 到控制器时遇到问题,显示错误,如 {"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"}。当我也调试时,在我的控制器中它不会传递给控制器。
这是我发布的内容
我在客户端使用 ReactJs,在服务器端使用 Net Core。这是我的代码sn-p:
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?}");
});
【问题讨论】:
-
你可以在没有
Content-Type的情况下尝试一下。当我处理表单数据时,它对我有用 -
不同的错误
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"0HLOGKVEBUTKP:00000008"}不支持的媒体类型@mkamranhamid -
我对.net一无所知,但看到你的错误,我猜你的目标资源应该被明确告知它应该在像这样
'Content-Type': 'multipart/form-data'的正文中接收什么样的数据 -
你也可以试试this
标签: javascript c# node.js reactjs asp.net-core