I’m suffering a lot trying to do this, in words: Trying to send a token to a myfunction.php so it can do it’s job, then make it return 2 variables so I can use em in my Javascript controller (Made with angularJS).
So far i heard I should use $http, but I can’t understand how I should do this
Advertisement
Answer
create an angularjs service and use it in your controller. here is the service which can provide Create, read, write and delete functionality:
app.service("MainService", ['$http', function ($http) {
    this.getItems = function (url) {
        return $http.get(url);
    };
    this.getItem = function (id, url) {
        return $http.get(url + "/" + id)
    };
    this.post = function (itemToCreate, url) {
        var request = $http({
            method: "post",
            url: url,
            data: itemToCreate
        });
        return request;
    };
    this.put = function (id, itemToChange, url) {
        var request = $http({
            method: "put",
            url: url + "/" + id,
            data: itemToChange
        });
        return request;
    };
    this.delete = function (id, url) {
        var request = $http({
            method: "delete",
            url: url + "/" + id
        });
        return request;
    };
}]);
next add this service as a dependency to your controller and use it’s methods like this:
app.controller("MyCtrl", ['$scope', '$rootScope', '$timeout', 'MainService', function ($scope, $rootScope, $timeout, MainService) {
    //Get All Items::
    $scope.GetItems = function () {
        var getAllItems = MainService.getItems("YOUR_API_URL_FOR_GET_ITEMS");
        getAllItems.then(function (response) {
            $scope.items = response.data;//use items with ng-repeat in you html code
        })
        .catch(function (response) {
            alert("YOUR_ERROR_MESSAGE");
        })
        .finally(function () {
            alert("AFTER_METHOD_EXECUTION");
        });
    };
    //Create New Item::
    $scope.Create = function () {
        //CREATE an Object and send it via post request. for example:
        var category = new CategoryObject($scope.Title, $scope.Description);
        var promisePost = MainService.post(category, "YOUR_API_URL_FOR_Post_Item");
        promisePost.then(function (response) {
            alert("SUCCESSFUL");
        })
        .catch(function (response) {
            alert("YOUR_ERROR_MESSAGE");
        })
        .finally(function () {
            alert("AFTER_METHOD_EXECUTION");
        });
    };
    //Edit an Item::
    $scope.Edit = function () {
        //CREATE an Object:
        var category = new CategoryObject($scope.Title, $scope.Description);
        //Then set it's Id
        category.Id = $scope.CategoryId;
        var promisePut = MainService.put($scope.CategoryId, category, "YOUR_API_URL_FOR_Put_Item");
        promisePut.then(function (response) {
            alert("SUCCESSFUL");
        })
        .catch(function (response) {
            alert("YOUR_ERROR_MESSAGE");
        })
        .finally(function () {
            alert("AFTER_METHOD_EXECUTION");
        });
    };
    //Delete an Item::
    $scope.delete = function (id) {
        var promiseDelete = MainService.delete(id, "YOUR_API_URL_FOR_Delete_Item");
        promiseDelete.then(function (response) {
            alert("SUCCESSFUL");
        })
        .catch(function (response) {
            alert("YOUR_ERROR_MESSAGE");
        })
        .finally(function () {
            alert("AFTER_METHOD_EXECUTION");
        });
    }
}]);
