【问题标题】:Sending Composite Types from JS to WCF REST Service将复合类型从 JS 发送到 WCF REST 服务
【发布时间】:2015-09-01 03:33:41
【问题描述】:

这里是代码sn-ps。

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/Angular http post调用

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. 结果: 调用成功,调用Service中的相关Register函数。但是收到的 NewUser 对象的所有属性都设置为 NULL。

这些值在 JS 方面是正确的,因此在调用之间它的某些东西是不正确的。 任何指针?我的猜测是我遗漏了一些明显且非常小的东西,但目前似乎无法弄清楚。

【问题讨论】:

  • 请忽略从 WCF 服务返回给 JS 的返回值,因为我正在纠正这些值。然而,核心问题是在请求部分收到 NULL。

标签: javascript c# angularjs wcf


【解决方案1】:

让它工作。这是所需的更改。

我们不需要使用 col 对象来包装用户对象,只需将用户对象传递给服务即可。所以不是

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

我们打电话

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

【讨论】:

    猜你喜欢
    • 2015-01-06
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多