Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
32
rated 0 times [  39] [ 7]  / answers: 1 / hits: 110705  / 10 Years ago, tue, december 16, 2014, 12:00:00

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)
.success(function (data){
$scope.data.products = data;
})
.error(function (error){
$scope.data.error=error;
console.log($scope.data.error.status); // Undefined!
// (This is the spot that I don't get it.)
});


If I code console.log($scope.data.error.status); , why does the argument of console.log is undefined?



In the book, there are sentence, The object passed to the error function defines status and message properties.



So I did $scope.data.error.status



Why is it wrong?


More From » angularjs

 Answers
1

Your arguments are incorrect, error doesn't return an object containing status and message, it passed them as separate parameters in the order described below.



Taken from the angular docs:




  • 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.



So you'd need to change your code to:



$http.get(dataUrl)
.success(function (data){
$scope.data.products = data;
})
.error(function (error, status){
$scope.data.error = { message: error, status: status};
console.log($scope.data.error.status);
});


Obviously, you don't have to create an object representing the error, you could just create separate scope properties but the same principle applies.


[#68471] Friday, December 12, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brennanm

Total Points: 510
Total Questions: 103
Total Answers: 95

Location: Nicaragua
Member since Tue, Dec 8, 2020
4 Years ago
brennanm questions
Thu, Jan 9, 20, 00:00, 5 Years ago
Thu, Sep 26, 19, 00:00, 5 Years ago
Thu, Aug 29, 19, 00:00, 5 Years ago
;