I am encountering an issue with passing JavaScript array objects through ajax post to my web API 2. Below is the code snippet where I am experiencing a null value in my web API:
<script>
$(document).ready(function () {
var Contacts = { steve: {},bill: {}, RON: {}, dan: {}, };
Contacts.bill = { Name: "Bill", UserName: "Gates", MobileNUmber: "(206) 555-5555",};
Contacts.steve = { Name: "Steve", UserName: "Jobs", MobileNUmber: "(408) 555-1111",};
Contacts.jon = { Name: "Steve", UserName: "Jobs", MobileNUmber: "(408) 555-2222", };
Contacts.RON = { Name: "Steve", UserName: "Jobs", MobileNUmber: "(408) 333-5555", };
Contacts.dan = { Name: "Steve", UserName: "Jobs", MobileNUmber: "(408) 444-5555",};
Contacts.qwe = { Name: "Steve", UserName: "Jobs", MobileNUmber: "(408) 555-5555",};
var ContactList = JSON.stringify(Contacts);
console.log(ContactList);
var baseUrl = "http://localhost:55942/";
$.ajax({
type: "POST",
data: ContactList,
url: baseUrl + 'api/CONTACT/comparecontacts',
contentType: "application/json"
});
});
</script>
// In my WEB API, I noticed that I am receiving Null value in my Contact list //
public class CONTACTController : ApiController
{
[HttpPost]
public IHttpActionResult comparecontacts(List<ContactList> ContactList)
{
return Ok();
}
}