admin管理员组文章数量:1422446
I am using foursquare api to grab longitude and latitude coordinates for searched venue, function looks like this:
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 specific tasks in case of success or error, so if it is a success case I want to return coordinates
if not print an error to the console, but not return coordinates
. I think I more or less got the error bit down, but can't figure success action (mented it out at the moment as it is called in success and error cases)
EDIT: seeing how this question can pop up in the future, what would be logic to check for success or failure of a http.post
?
I am using foursquare api to grab longitude and latitude coordinates for searched venue, function looks like this:
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 specific tasks in case of success or error, so if it is a success case I want to return coordinates
if not print an error to the console, but not return coordinates
. I think I more or less got the error bit down, but can't figure success action (mented it out at the moment as it is called in success and error cases)
EDIT: seeing how this question can pop up in the future, what would be logic to check for success or failure of a http.post
?
2 Answers
Reset to default 4In fact the subscribe method allows to register callbacks for success and factures. Perhaps arrow functions disturb you a bit:
this.http.get(...)
.map(res => res.json())
.subscribe(
data => {
// Processing for successfull response
},
error => {
// Processing for failures
}
);
You can only return the observable (for example from a service) but not a result because HTTP requests are asynchronous. Then you can subscribe on it within ponents for example and set ponent attributes to be displayed in the view.
Hope it helps you. Thierry
you can chain the http.get()
with a then
function, where you can put the success and error callbacks:
this.http.get(URL+params).then(function(data){
// success
}, function(data){
// error
});
本文标签: javascriptHandling success and error responses from httpget in angular 2Stack Overflow
版权声明:本文标题:javascript - Handling success and error responses from http.get in angular 2 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745358403a2655182.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论