该问题可能是由于ASP.NET Core Web API中未正确设置Request Header所导致的。可以通过以下方式设置Header:
在Server端:
使用Middleware,在Configure方法中添加如下代码:
app.Use(async (context, next) => { context.Request.Headers["Content-Type"] = "application/json"; await next.Invoke(); });
或者可以在Controller的Action中设置Header:
[HttpPost]
public async Task
在Client端:
使用$.ajax来发送POST请求,并正确设置ContentType:
$.ajax({ url: "/MyController/MyAction", type: "POST", data: JSON.stringify({ myData: "some data"}), contentType: "application/json", success: function (result) { console.log("Success", result); }, error: function (xhr, status, error) { console.log("Error", error); } });