0

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);
    });

}

2 Answers 2

1

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,
        })

    };
Sign up to request clarification or add additional context in comments.

3 Comments

i have added the , previleges but the same error exist
@Sajeetharan Please note previleges != privileges
var promisePost = Permissions.save("UserPermission/save", previleges);
0
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,
             })
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.