Angular $http difference between .success() and .then()

后端 未结 4 1052
半阙折子戏
半阙折子戏 2021-01-24 23:47

Sometimes I have trouble to fetch my data using $http service. And I did not 100% understand the main difference between .success() and .then()

The following is an examp

4条回答
  •  悲&欢浪女
    2021-01-24 23:58

    As per the documentation: https://docs.angularjs.org/api/ng/service/$http

    The .then() function is the standard way to register callbacks for when a promise has been resolved - see https://docs.angularjs.org/api/ng/service/$q for angular's implementation of promises.

    The promise returned by $http is a promise for a Response:

    $http.get('http://example.com').then(function successCallback(response) {
    response.data, response.status, and so on
    })
    

    Oftentimes, all you need from the Response is just the data. The callbacks registered via .success() will receive just data:

    $http.get('http://example.com').success(function successCallback(data) {
    data.??? depending on what you return
    })
    

提交回复
热议问题