我正在打电话给这样的服务,
var ms = [], url = myServer + "/government.svc/Users/Add";
ms.push({
ID: 210,
UserID: "S-1-691534-18",
Role: "1",
StartDate: myUser.startDate,
UpdateM: true
});
var user = {
Email: myUser.desc,
FirstName: myUser.FN,
LastName: myUser.LN,
Memberships: ms,
.. added all other properties like this "PropertyName" : ""
};
console.log(JSON.stringify(user));
$.ajax({
url: url,
type: "POST",
data: JSON.stringify({ pUser: user }) + '}',
contentType: "application/json; charset=utf-8"
})
在stringify之后,我在控制台
中得到这个"{"pUser":
{
"ID":"S-1-691534-18",
"Email":""user.one@google.com",
"FirstName":"User",
"LastName":"One",
"Memberships":[{"ID":"210","UserID":"S-1-691534-18","Role":"1","StartDate":"04/09/2015","UpdateM":true}],
"Username":"",
"Phone":"",
"Mobile":"",
"AddressID":"",
"Address":"",
"Photo":"",
"Bio":"",
"Notes":"",
"EmailNotes":"",
"IsNaugthy":"",
"CurrentMembershipID":"",
"MembershipStatus":"",
"OtherDate":"",
"MemAckwww":"",
"EatHabbits":""
}
}"
我的服务器端方法,
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/Users/Add")]
UserDetails AddUser(User pUsers);
错误
400 - 请求不好。
我在WCF中调试服务器端时为pUser获取null,所以我认为我需要将以上var用户对象作为用户对象发送而不是长字符串?
我的WCF数据合同
我的服务器端有更多DataMembers然后我发送的内容,我不知道这是怎么产生问题的?
我是否必须匹配客户端代码中的所有成员才能使其正常工作?
修改
我的服务器端类,
public class User
{
[DataMember]
public string ID;
[DataMember]
public string Username;
[DataMember]
public string FirstName;
[DataMember]
public string LastName;
[DataMember]
public string Phone;
[DataMember]
public string Mobile;
[DataMember]
public string Email;
[DataMember]
public int? AddressID;
[DataMember]
public string Address;
[DataMember]
public string Photo;
[DataMember]
public string Bio;
[DataMember]
public string Notes;
[DataMember]
public string EmailNotes;
[DataMember]
public int IsNaugthy;
[DataMember]
public int CurrentMembershipID;
[DataMember]
public string MembershipStatus;
[DataMember]
public string OtherDate;
[DataMember]
public int? MemAckwww;
[DataMember]
public Membership[] Memberships;
[DataMember]
public Eat[] EatHabbits;
}
答案 0 :(得分:1)
您的操作需要参数pUsers
。您正在发送一个参数名为pUser
的对象。
从
更改服务器端方法[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/Users/Add")]
UserDetails AddUser(User pUsers);
到
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/Users/Add")]
UserDetails AddUser(User pUser); //changed parameter name
另外,确保在发送数据时,模型中任何非可空类型的属性都具有相应的值,否则模型绑定器会感到不安。