admin管理员组文章数量:1424147
Hi In my Angular Component, i have this code in one of my methods
this.http.get("http://localhost:8080/poeples")
.map(
resp => { resp = resp.json(); }
).subscribe(
(data) => { this.poeples = data; },
err => console.log(err)
);
In network tab in chrome dev inspector i saw that my get call returning result, but data
is undefined.
Why?
Hi In my Angular Component, i have this code in one of my methods
this.http.get("http://localhost:8080/poeples")
.map(
resp => { resp = resp.json(); }
).subscribe(
(data) => { this.poeples = data; },
err => console.log(err)
);
In network tab in chrome dev inspector i saw that my get call returning result, but data
is undefined.
Why?
Share Improve this question edited Mar 12, 2018 at 22:36 Zze 18.9k14 gold badges95 silver badges125 bronze badges asked Mar 12, 2018 at 22:31 shmoolkishmoolki 1,5619 gold badges35 silver badges58 bronze badges 1- 1 Because of asynchronicity. – Learn on hard way Commented Mar 12, 2018 at 22:33
3 Answers
Reset to default 4The reason it was not working originally, is because you had this:
resp => { resp = resp.json(); }
You are not returning a value. When you use the curly braces, you have to explicitly define a return value. All you had to do was:
resp => { return resp.json(); }
Or remove the braces:
resp => resp.json()
// Your code that isn't working:
/*this.http.get("http://localhost:8080/poeples")
.map(
resp => {
resp = resp.json()
}
).subscribe(
(data) => {
this.poeples = data;
},
err => console.log(err)) ;*/
// Working code:
@import { map } from 'rxjs/operators';
this.http.get('http://localhost:8080/poeples')
.pipe(
// In your example, you are creating a new function with the {} wrapped around this line, but you aren't returning anything, so the return value of the "data" below bees "undefined."
map((response) => response.json())
)
.subscribe(
(data) => {
this.poeples = data;
},
(err) => console.log(err)
);
Error - when subscribing request success code not working. This is my code I'm working on
this.userService.addDeliverDetails(form.value) .subscribe( res=>{ this.have=true; }, error=>{ console.log('error') } );
Try to console.log() in error and if it logged that's mean your data ing from the server as text not JSON formatted.
Two solutions 1) change angular to accept text responses 2) change server response to json not to string (plain text)
本文标签: javascriptAngular Http Subscribe not workingStack Overflow
版权声明:本文标题:javascript - Angular Http Subscribe not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744639671a2617043.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论