In angular $http service, How can I catch the “status” of error?

前端 未结 6 2046
南笙
南笙 2020-12-08 04:40

I\'m reading a book called, \"Pro Angular JS\". However, I have a question about how to catch a status of error.

What I coded is :

$http.get(dataUrl)         


        
6条回答
  •  隐瞒了意图╮
    2020-12-08 04:56

    The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. Have a look at the docs https://docs.angularjs.org/api/ng/service/$http

    Now the right way to use is:

    // Simple GET request example:
    $http({
      method: 'GET',
      url: '/someUrl'
    }).then(function successCallback(response) {
        // this callback will be called asynchronously
        // when the response is available
      }, function errorCallback(response) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
    });
    

    The response object has these properties:

    • data – {string|Object} – The response body transformed with the transform functions.
    • status – {number} – HTTP status code of the response.
    • headers – {function([headerName])} – Header getter function.
    • config – {Object} – The configuration object that was used to generate the request.
    • statusText – {string} – HTTP status text of the response.

    A response status code between 200 and 299 is considered a success status and will result in the success callback being called.

提交回复
热议问题