Reputation: 2542
I am trying to call a simple method with ajax as follow:
[Route("v1/[controller]")]
[ApiController]
public class FooController : Controller
{ [HttpPost]
public FooDTO Add(Foo Foo)
{
FooDTO objFooDTO = null;
}
}
public class Foo
{
public int ID {get; set;}
public string Title {get; set;}
}
Ajax request is as follow:
var obj = {
"ID": 0,
"Title": "Foo Bar"
}
$.ajax({
type: "POST",
url: "http://localhost:8001/v1/Foo",
data: obj,
async: false,
dataType: "Json",
crossDomain: true,
contentType: 'application/x-www-form-urlencoded',
cache: false,
success: function (resp) {
},
error: function (e) {
}
});
Its working fine with POSTMAN
but when I try to hit via ajax error is 400
Bad request The input is not valid
. Is there anything which I am missing ? jquery
version is 1.12.0
.
Upvotes: 0
Views: 546
Reputation: 174
Looks like the contentType
property supposed to be application/json
as you are passing on an object but not form data.
Also, you should serialize data object with JSON.Stringify
method so that the javascript object will be converted to json.
Upvotes: 1