3

我有一个控制器,它使用以下行通过名为的工厂将数据发布到服务器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...

因为角度文档说

$http 遗留承诺方法成功和错误已被弃用。请改用标准 then 方法。如果 $httpProvider.useLegacyPromiseExtensions 设置为 false,那么这些方法将抛出 $http/legacy 错误。

我需要什么

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

4

2 回答 2

4

我想这就是你的意思:

$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` */ });
于 2015-09-24T18:02:30.063 回答
4

当它被拒绝时,拒绝承诺而不是解决它$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返回一个 Promise,它可以通过使用 Promise 链接进一步简化。这样就不需要使用额外的延迟对象。

/**
 * 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.
    }
  );
}
于 2015-09-24T18:13:20.053 回答