我将JavaScript数组对象通过ajax帖子传递到我的web API 2下面是我的代码,我在我的web API中获得null值
<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>
// WEB API我在联系人列表中获得Null值//
public class CONTACTController : ApiController
{
[HttpPost]
public IHttpActionResult comparecontacts(List<ContactList> ContactList)
{
return Ok();
}
}
答案 0 :(得分:0)
因为您没有发布联系人数组。您正在发布具有不同联系人的属性名称的对象。尝试将您的js更改为:
$(document).ready(function () {
var contacts = [];
contacts.push({ Name: "Bill", UserName: "Gates", MobileNUmber: "(206) 555-5555", });
contacts.push({ Name: "Steve", UserName: "Jobs", MobileNUmber: "(408) 555-1111", });
//..
var baseUrl = "http://localhost:55942/";
$.ajax({
type: "POST",
data: JSON.stringify(contacts),
url: baseUrl + 'api/CONTACT/comparecontacts',
contentType: "application/json"
});
});