AngularJS:在服务中作为null到达的值

时间:2015-05-12 13:46:39

标签: angularjs service asp.net-web-api

我正在尝试将对象传递给我的API,但是当我因某种原因执行此操作时,该对象变为null。我无法看到这个原因,因为我在其他多个场合都做过类似的过程。

        var promiseGetRole = loginService.GetRole(user);
        promiseGetRole.then(function (data, status, headers, config) {

            $location.path('/UserManagement').replace();
            if (!$scope.$$phase) $scope.$apply()

        },
        function (errorResult) {
            console.log("Unable to log in : " + errorResult);
        });

用户已正确填充。

//Get the Role of a given user
this.GetRole = function (user) {
    return $http.get(toApiUrl('login'), user);
}

此时,用户已正确填充。

    //The user has been validated, now retrieve their role from the server
    public string Get(UserLogin user)
    {

        string role = "";

        //TODO: set role

        return role;

    }

此时,当它到达API时,User的值才变为null。我在post方法中使用相同的进程(使用UserLogin作为传入的对象)并且看不到它们之间的任何差异。

1 个答案:

答案 0 :(得分:0)

我会像这样将数据传递给后端(假设您可以通过userId检索角色):

var promise = $http.get(toApiUrl('login'), { params:{userId:user.Id}); // Only pass id in queryparams
... 

您可能可以在查询字符串中传递整个用户对象,但它有一个长度限制。

我不知道你后端运行的是什么,这取决于你可能需要做不同的事情来使它绑定到数据。