[C#]将复合类型从 JS 发送到 WCF REST 服务

发布时间: 2017/2/24 2:51:21
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

这里的代码片段。

1.复杂的对象︰

[DataContract]
public class NewUser
{
    [DataMember(Name = "Email")]
    public string Email { get; set; }
    [DataMember(Name = "FirstName")]
    public string FirstName { get; set; }
    [DataMember(Name = "LastName")]
    public string LastName { get; set; }
    [DataMember(Name = "Mobile")]
    public string Mobile { get; set; }
    [DataMember(Name = "UserAddress")]
    public Address UserAddress { get; set; }
}

2.经营合同

[OperationContract]
[WebInvoke(UriTemplate = "/RegisterUser", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, Method = "POST")]
Guid Register(NewUser col);

3.JS/角 http 帖子调用

var newUser = {};

newUser.Email = $scope.col.Email;
newUser.FirstName = $scope.col.FirstName;
newUser.LastName = $scope.col.LastName;
newUser.Mobile = $scope.col.Mobile;
newUser.CityId = $scope.col.UserAddress.CityId;


$http.post(API_URL + "/RegisterUser", { col: newUser }).success(function (response) {
    if(response.status=="success" && !response.mailError){
        $scope.register = false;
        $scope.alert = {type:'success', msg:"You are successfully registered. Please check mail for the password. If you do not see the mail, check spams"};
    }else if(response.mailError){
    $scope.alert = {type:'warning', msg:'Unable to send mail! please try forgot password next time you need to login'};
    }else{
        $scope.alert = {type:'danger', msg:response.msg};
    }
    $scope.loading = false;
});
  1. 结果︰ 该调用是成功的在服务相关的注册函数被调用。不过收到一个对象具有所有属性设置为 NULL。

所以它的东西这是不正确的调用之间的值是正确从 JS 侧。 任何指针吗?我猜是我失去了明显和很小的东西,但似乎不能找出那一刻。

解决方法 1:

得到了它的工作。这里是所需的更改。

而不是使用一个 col 对象来包装该用户对象,我们只需要将用户对象传递给该服务。而不是这样

$http.post(API_URL + "/RegisterUser", { col: newUser }).success(function (response) {

我们称之为

$http.post(API_URL + "/RegisterUser", newUser).success(function (response) {
赞助商