[英]415 Unsupported Media Type Error being returned when posting to API through C# class
[英]415 unsupported media type error when posting to aspnetcore api from angular using $http
在Angular中使用以下方式调用:
function Login(email, password, callback)
{
var GetAll = new Object();
GetAll.email = email;
GetAll.password = email;
$http({
url: "http://localhost:52587/api/TokenAuth/Login",
dataType: 'json',
method: 'post',
data: JSON.stringify(GetAll),
headers: {
'Content-Type': 'application/json'
}
})
.then(function loginSuccessCallback(response)
{...
我已经使用了不错的swagger和ajax进行了测试。 我使用主体中的json对象对其进行了设置(设置为raw和JSON(application / json))。
我已经编写了以下Web API:
namespace WEBAPI.Controllers
{
public class user
{
public string email { get; set; }
public string password { get; set; }
}
[Produces("application/json")]
[Route("api/[controller]")]
public class TokenAuthController : Controller
{
[HttpPost("Login")]
public async Task`<IActionResult>` Login([FromBody]user usr)
{
..................
..................
}
}
}
但是我得到[FromBody] 415不支持的媒体类型,并且没有[FromBody]获得null参数。 请帮我
有人看到我要去哪里了吗?
不需要JSON.stringify您的数据,因为您真正需要的是JSON对象。 将数据设置为字符串肯定会导致415(不支持的媒体类型)错误。 你可以这样
$http({
url: "http://localhost:52587/api/TokenAuth/Login",
dataType: 'json',
method: 'post',
data: GetAll,
headers: {
'Content-Type': 'application/json'
}
})
.then(function loginSuccessCallback(response)
您可以尝试添加accept
标头
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.