1

find my below code for delete function its deleting in backend and returning success message to service from that not returning to controller ,so was unable to print growl message.

This is my controller side code:

$scope.deleteBlogswithid = function(id) {
                                  var loggedInUserId = $rootScope.loggedInUser.id;

                                  if ($rootScope.loggedInUser != null) {


                                    blogService.deleteBlog(id, loggedInUserId,function(data) {
                                        if(data == 'success'){

                                                $location.path("/home");
                                        $growl.box('Blog has been deleted', {  
                                            class : 'danger',
                                            sticky : false,
                                            timeout : 5000
                                        }).open();
                                        }
                                            })
                                } else {
                                    $growl.box('Please login to delete the blog', {  
                                                class : 'danger',
                                                sticky : false,
                                                timeout : 5000
                                            }).open();
                                }

                            }

service.js:

blogbeatsApp.service('blogService', function(httpService) {
    this.deleteBlog = function(id,loggedInUserId,data, callback) {
        var url = 'blog/delete/' +id + "/" + loggedInUserId;
        httpService.postRequest(url,data, callback);
    };
});

httpService.js:this is my httpservice

blogbeatsApp.service('httpService', function($http, $location, $rootScope){
    this.postRequest = function(url, data, contentType, callback){
        $http({
            method : 'POST',
            url : url,
            data : data,
            headers : {
                'Content-Type' : contentType
            }
        }).success(function(data) {
            callback(data);
        }).error(function(data, status, headers, config) {

        });
    };

i am not getting success message to controller in function(data).please help

2
  • 1
    You does not pass parameter in correct order. httpService.postRequest(url,data, callback); your callback is 3rd parameter of postRequest. Which is bind with function(url, data, contentType, callback) - contentType since its your 3rd parameter. use this in your httpService this.postRequest = function(url, data,callback, contentType) callback must be 3rd parameter Commented Sep 9, 2015 at 7:31
  • yes..now works.thank u Commented Sep 9, 2015 at 8:05

1 Answer 1

1

As I say in comments.

You does not pass parameter in correct order.

your callback is 3rd parameter of postRequest. Which is bind with function(url, data, contentType, callback) - contentType.

Since, its your 3rd parameter use this in your httpService as this.postRequest = function(url, data,callback, contentType)

callback must be 3rd parameter

change httpSerivce :

blogbeatsApp.service('httpService', function($http, $location, $rootScope){
this.postRequest = function(url, data, contentType, callback){
    $http({
        method : 'POST',
        url : url,
        data : data,
        headers : {
            'Content-Type' : contentType
        }
    }).success(function(data) {
        callback(data);
    }).error(function(data, status, headers, config) {

    });
};

To :

blogbeatsApp.service('httpService', function($http, $location, $rootScope){
this.postRequest = function(url, data, callback, contentType){
    // callback should be as 3rd parameter. 
    $http({
        method : 'POST',
        url : url,
        data : data,
        headers : {
            'Content-Type' : contentType
        }
    }).success(function(data) {
        callback(data);
    }).error(function(data, status, headers, config) {

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

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.