I'm starting with web api. I'm trying to do a POST call.
I have my controller:
[HttpPost]
public myData test(myData m)
{
return m;
}
I created the class myData inside models. BTW, should it be inside Views/ModelView?
public class myData
{
public int Id { get; set; }
public string Name { get; set; }
}
Than, I have an HTML file that calls this POST:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<script>
var model = {
Name: "Shyju",
Id: 123
};
$.ajax({
type: "POST",
dataType: "jsonp",
data: JSON.stringify(model),
url: "http://localhost:52884/api/contact",
contentType: "application/json"
}).done(function(res) {
console.debug(res);
// Do something with the result :)
}).fail(function(error) {
console.debug(error);
});
</script>
</body>
</html>
*I edited the html. The Cross-Origin error was fixed with dataType: "jsonp".
My problem now is that it goes to the fail() method from AJAX, but the console returns:
status: 200
statusText: success
readyState: 4
It should return the data from my var model. Right?
Thanks