如何通过Angularjs服务发送对象和数组

时间:2018-05-06 08:12:23

标签: angularjs rest api

我正在尝试将对象和数组同时发送到API,但是我收到了错误

这是对象(来自输入框)

 var vacation = {
            Vac_Main_Key: $scope.vackey, Vac_Main_Code: $scope.code, Gender_Type: $scope.gen, CareeService_Flag: $scope.career, Vac_Duration: $scope.vduration,
            Duration_Flag: $scope.vflag}

这是数组(来自多个复选框)

 $scope.selectedcontract = function (con)
    {
        if (con.details == true) {
            $scope.vacationcontracts.push({ VacMKey: $scope.vackey, WType: con.sys_key });
            console.log($scope.vacationcontracts);
        }
        else if (con.details == false) {
            $scope.vacationcontracts.splice(con, 1);
        }

    }

保存按钮

var promisePost = vacationsetupSrv.save(vacation, $scope.vacationcontracts);
        promisePost.then(function () {}

angularjs服务:

var vacationsetup = angular.module("vacationsetup", [])
                        .service("vacationsetupSrv", function ($http) {

                            var urlBase = "/VacationSetupAPI/api";



                            this.save = function (url, vacation,vacationcontracts) {
                                return $http({
                                    method: "POST",
                                    url: urlBase + '/' + url,
                                    data: vacation, vacationcontracts,
                                    async: false,
                                })

                            };

我收到此错误http://localhost/VacationSetupAPI/api/[object%20Object] 任何帮助,提前致谢

1 个答案:

答案 0 :(得分:0)

尝试

this.save = function (vacation,vacationcontracts) {

您发送 object 作为第一个参数,然后将其作为字符串分配给 url ,这是错误的。

vacationsetupSrv.save(vacation, $scope.vacationcontracts);

尝试

vacationsetupSrv.save(“”,vacation, $scope.vacationcontracts);