我有一个控制器,它使用以下行通过名为的工厂将数据发布到服务器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...
成功,然后根据成功或失败做一些事情。我怎样才能做到这一点?