angular JS 1.6アップデートで$httpが動かなくなった件

Angular JS 1.6が2016年12月にリリースされたようです。

プロジェクトを Angular JS 1.5から Angular JS 1.6へアップデートしたところ、 次のようなエラーが・・・

TypeError: $http(…).success is not a function

どうやら1.6から $http(…).success が使えなくなってしまったようです。

公式ページの説明によると

Due to b54a39, $http’s deprecated custom callback methods – success() and error() – have been removed. You can use the standard then()/catch() promise methods instead, but note that the method signatures and return values are different.

success(fn) can be replaced with then(fn), and error(fn) can be replaced with either then(null, fn) or catch(fn).

なるほどよくわかりませんが、 .successとか .errorが使えなくなったようです。
代わりにonSuccessとかonErrorを使えばOKみたい。
修正は簡単です。

Angular JS 1.5までのソースコード

var deferred = $q.defer();

$http({
  method: 'POST',
  url: "https://~~~",
  data: parameter,
  timeout: 5000
}).
success(function onSuccess(data, status, headers, config) {
  // 通信成功
  deferred.resolve(data);

}).
error(function onError(data, status, headers, config) {
  // 通信失敗
  deferred.reject(0);
});
return deferred.promise;

Angular JS 1.6ではこんな感じになります

var deferred = $q.defer();

$http({
  method: 'POST',
  url: "https://~~~",
  data: parameter,
  timeout: 5000
}).
then(function onSuccess(response) {
    deferred.resolve(response.data);
}, function onError(response) {
    //通信に失敗
    deferred.reject(0);
});
return deferred.promise;

すこし簡潔に書けるようになりましたね?

ん〜微妙か