2

Doesn't matter whether I am posting or deleting etc.. I would write this:

$http({
    method: ''
    url: '',
    data: '',
    headers: ''
}).success(function(data, status, headers, config) {
    //whatever happens on success
}).error(function(data, status, headers, config) {
    //whatever happens on failure
});

what is the difference between doing the above or doing this:

$http({
    method: ''
    url: '',
    data: '',
    headers: ''
}).then(function(response) {
    //success
},
    function(response) {
        //failed
};

Does one have advantages over the other? Did one come about later and there isn't much of a difference? Just want to know the differences really and what people prefer to use and why.

user2405469
  • 1,953
  • 2
  • 22
  • 43
  • 1
    possible duplicate of [Angular HttpPromise: difference between \`success\`/\`error\` methods and \`then\`'s arguments](http://stackoverflow.com/questions/16385278/angular-httppromise-difference-between-success-error-methods-and-thens-a) – Anthony Chu Jul 22 '14 at 16:09

2 Answers2

0

From the $http docs:

Returns a promise object with the standard then method and two http specific methods: success and error. The then method takes two arguments a success and an error callback which will be called with a response object. The success and error methods take a single argument - a function that will be called when the request succeeds or fails respectively. The arguments passed into these functions are destructured representation of the response object passed into the then method.

Which means that both examples you posted actually do the same, since .success(function () { ... }) and .error(function () { ... }) are just aliases of .then(function () { ... }) and `.then(null, function () { ... }), respectively.

Danilo Valente
  • 11,270
  • 8
  • 53
  • 67
0

I believe that the .then() syntax came later, as everything was standardized on the $q API. .success() and .error() were probably left for backward-compatibility.

See the Changelog for 0.10.6 where it says "the $xhr service was replaced with $http with promise based apis."

Matthias
  • 13,607
  • 9
  • 44
  • 60