"Incorrect Content-Type: " exception throws angular mvc 6 application
The accepted answer didn't work for me. For me, the solution was to add a header to the $http call:
$http({
url: "/install",
method: "POST",
data: data,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
})
.success(...);
In my case, adding [Produces("application/json")]
did nothing, but adding the [FromBody]
attribute to the parameter is what did the trick!
Hope the following examples helps you.
Try to decorate your controller with
[Produces("application/json")]
[Route("api/[controller]")]
Since you din't show your controller name I will give you a fictitious full working example
controller
[Produces("application/json")]
[Route("api/[controller]")]
public class DashBoardLayoutApi : Controller
{
public DashBoardLayoutApi()
{ }
[HttpPost]
public void Post([FromBody] LoginViewModel data)
{ }
}
C# viewmodel
public class LoginViewModel
{
public string Email { get; set; }
public string Password { get; set; }
}
HTML/JS
<script>
var data = {
Email: 'Test',
Password: 'Test',
RememberMe: true
};
$("#test").click(function() {
$.ajax({
url: '/api/DashBoardLayoutApi',
type: 'POST',
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(data),
});
}
</script>
<button id="test"> Save Layout</button>
Results