【发布时间】:2015-05-03 15:23:58
【问题描述】:
这是我的客户代码
var sayeed = { firstname: "Sayeed", surname: "Alquiaty" };
alert(JSON.stringify({ person: sayeed }));
$.ajax({
url: "api/parent",
type: "POST",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ person: sayeed }),
success: function(response) {
response ? alert("It worked!") : alert("It didn't work.");
}
});
在服务器端
public class Person {
public string Firstname { get; set; }
public string Surname { get; set; }
}
//Here I am able to receive object but is is not FirstName = null and Surname = null
// POST api/parent
public bool PostParent(Person person) {
return person != null;
}
所以客户端收到一条成功消息但实际上 JSON 对象不是反序列化
我尝试过其他方法,例如使用 JObject,但这会破坏 Json 对象,我的意思是将客户端对象转换为 Key,然后添加:“”。这不是我想要的。
【问题讨论】:
-
如果您只发布“{'firstname':'Sayeed','surname':'Alquiaty'}”会发生什么
-
嗨尼克,我试过了,所以我的人对象为空。
-
如果你在第一行大写你的参数名称会怎样: var sayeed = { Firstname: "Sayeed", Surname: "Alquiaty" };
标签: c# json asp.net-web-api2