Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know from controller if http.post().then... was successful?

I have a controller which uses the following line to post data to server through a factory called SendDataFactory:

SendDataFactory.sendToWebService(dataToSend)

And my factory SendDataFactory looks like this:

angular
  .module('az-app')
  .factory('SendDataFactory', function ($http, $q) {

    var SendData = {};

    /**
     * Sends data to server and
     */
    SendData.sendToWebService = function (dataToSend) {

      var url = "example.com/url-to-post";
      var deferred = $q.defer();

      $http.post(url, dataToSend)

        //SUCCESS: this callback will be called asynchronously when the response is available
        .then(function (response) {
          console.log("Successful: response from submitting data to server was: " + response);

          deferred.resolve({
            data: data
          });
        },

        //ERROR:  called asynchronously if an error occurs or server returns response with an error status.
        function (response) {
          console.log("Error: response from submitting data to server was: " + response);

          deferred.resolve({
            data: data
          });
        }
      );

      return deferred.promise;
    }

    return SendData;
  });

I have seen some examples in here and the internet with

$http.post().success...

but I want to use

$http.post().then...

since angular documentation says:

The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error.

What I need:

Now in my controller I need to check if the $http.post().then... was successful or not and then do something based on success or fail. How can I achieve this?

like image 893
CommonSenseCode Avatar asked Jan 25 '26 17:01

CommonSenseCode


1 Answers

I think this is what you meant:

$http.post(url, dataToSend)

    //SUCCESS: this callback will be called asynchronously when the response is available
    .then(function (response) {
      console.log("Successful: response from submitting data to server was: " + response);

      deferred.resolve({
        data: response //RETURNING RESPONSE SINCE `DATA` IS NOT DEFINED
      });
    },

    //ERROR:  called asynchronously if an error occurs or server returns response with an error status.
    function (response) {
      console.log("Error: response from submitting data to server was: " + response);

      //USING THE PROMISE REJECT FUNC TO CATCH ERRORS
      deferred.reject({
        data: response //RETURNING RESPONSE SINCE `DATA` IS NOT DEFINED
      });

    }
  );

  return deferred.promise;
}

In your controller you now can use:

SendDataFactory.sendToWebService(dataToSend)
    .then(function(data) { /* do what you want */ })
    .catch(function(err) { /* do what you want with the `err` */ });
like image 174
udidu Avatar answered Jan 28 '26 09:01

udidu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!