Handling success and error responses from http.get in angular 2

I use the foursquare api to capture the coordinates of longitude and latitude for the location found, the function is as follows:

getCoordinates(params) {
   this.http.get(URL+params)
    .map(res => res.json())
    .subscribe(
      data => this.coordinates = data,
      err => this.logError(err)
    );
   // return coordinates;
  }

I am trying to figure out how to perform certain tasks in case of success or error, therefore, if this is a success case, I want return coordinates, if not to print the error on the console, but not return coordinates. I think that I more or less got an error, but I can’t determine the effect of success (commented on it at the moment, since it is called in case of success and errors)

EDIT: Seeing how this question may appear in the future, what will be the logic for checking success or failure http.post?

+4
2

subscribe . , :

this.http.get(...)
  .map(res => res.json())
  .subscribe(
    data => {
      // Processing for successfull response
    },
    error => {
      // Processing for failures
    }
  );

(, ), , HTTP- . , , , .

, . Thierry

+4

http.get() then, :

this.http.get(URL+params).then(function(data){
    // success
}, function(data){
    // error
});
0

Source: https://habr.com/ru/post/1624238/


All Articles