简体   繁体   中英

Angularjs $http service error

i have an error between angularjs controller and $http services The error is saying that the privileges is not defined. i am trying to post an object to an API any idea or help, thanks in advance

 var userpermissions = angular.module("userpermissions", [])
                        .service("Permissions", function ($http) {
                            var urlBase = "/UserPermissionAPI/api";


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

                            };

                        })
.controller("userpermission", function ($scope, Permissions) {

$scope.insert = function () {
        var promisePost = Permissions.delete("UserPermission/delete?staffkey=" + $scope.staffkey + '&module=' + $scope.modulecd);
        promisePost.then(function (pl) {


        var privileges = {
            Staff_Key: $scope.staffkey, Update_Per: $scope.updates, Save_Per: $scope.saves, Delete_Per: $scope.deletes, Search_Per: $scope.searches,
            Add_Admin_User: $scope.staffkeyurl, Module_Code: $scope.modulecd, Report_Per: $scope.reports
           };

        var promisePost = Permissions.save("UserPermission/save");
        promisePost.then(function () {
            toastr.success("Successfully saved");
        })

    }, function (err) {
        console.log("Err" + err);
    });

}

You are not passing previleges anywhere in your service, change it as

  var privileges = {
            Staff_Key: $scope.staffkey, Update_Per: $scope.updates, Save_Per: $scope.saves, Delete_Per: $scope.deletes, Search_Per: $scope.searches,
            Add_Admin_User: $scope.staffkeyurl, Module_Code: $scope.modulecd, Report_Per: $scope.reports
           };

 var promisePost = Permissions.save("UserPermission/save", previleges);

and the method inside the service to accept previleges,

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

    };
var promisePost = Permissions.save("UserPermission/save");

This line needs to be changed.

In this line if you send privileges object as a one more parameter and change your save function to accept it, then this will work. Check below.

var promisePost = Permissions.save("UserPermission/save", privileges);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM