如何从控制器知道 http.post().then... 是否成功?

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

我有一个控制器,它使用以下行 post 通过名为 SendDataFactory 的工厂将数据发送到服务器:

SendDataFactory.sendToWebService(dataToSend)

我的工厂 SendDataFactory 看起来像这样:

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

我在这里和互联网上看到了一些例子

$http.post().success...

但我想使用

$http.post().then...

因为 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.

我需要什么:

现在在我的控制器中,我需要检查 $http.post().then... 是否成功,然后根据成功或失败做一些事情。我怎样才能做到这一点?

我想这就是你的意思:

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

在您的控制器中,您现在可以使用:

SendDataFactory.sendToWebService(dataToSend)
    .then(function(data) { /* do what you want */ })
    .catch(function(err) { /* do what you want with the `err` */ });

当它被 $http 拒绝时,拒绝而不是解决它。

/**
 * 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(response.data);  // Resolving using response.data, as data was 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);


      deferred.reject(response.data);  // Rejecting using response.data, as data was not defined.
    }
  );

  return deferred.promise;
}

然后您可以从您的控制器调用它,就像您使用 then 在服务中处理回调一样。

因为 $http returns 一个承诺,它可以通过使用承诺链进一步简化。这样就不需要使用额外的延迟对象。

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

  var url = "example.com/url-to-post";

  return $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);

      return response.data;  // Resolving using response.data, as data was 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);


      return $q.reject(response.data);  // Rejecting using response.data, as data was not defined.
    }
  );
}