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

前端 未结 6 2052
南笙
南笙 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:46

    UPDATED: As of angularjs 1.5, promise methods success and error have been deprecated. (see this answer)

    from current docs:

    $http.get('/someUrl', config).then(successCallback, errorCallback);
    $http.post('/someUrl', data, config).then(successCallback, errorCallback);
    

    you can use the function's other arguments like so:

    error(function(data, status, headers, config) {
        console.log(data);
        console.log(status);
    }
    

    see $http docs:

    // Simple GET request example :
    $http.get('/someUrl').
      success(function(data, status, headers, config) {
        // this callback will be called asynchronously
        // when the response is available
      }).
      error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
      });
    

提交回复
热议问题