解析Angular HTTP响应并处理JSON。

huangapple go评论89阅读模式
英文:

Parse Angular Http Reponse And Handle Json

问题

  1. 显示 = 响应 => {
  2. console.log(响应);
  3. };
  1. 导入 { Component, OnInit, OnDestroy } '@angular/core';
  2. 导入 { HttpClient } '@angular/common/http';
  3. 导入 { of, Subject } 'rxjs';
  4. 导入 { delay, tap, mergeMap, repeat, takeUntil } 'rxjs/operators';
  1. export class AppComponent 实现 OnInit, OnDestroy {
  2. 私有 ngUnsubscribe = 主体<void>(); // 用于取消订阅的主体
  3. 构造函数(private http: HttpClient) { }
  4. callOmni = () => of(this.pollOmni()).pipe();
  5. 显示 = 响应 => {
  6. console.log(响应);
  7. };
  8. 轮询 = of({}).pipe(
  9. mergeMap(_ => this.callOmni()),
  10. tap(this.display),
  11. 延迟(10),
  12. 重复(),
  13. takeUntil(this.ngUnsubscribe) // 当 ngUnsubscribe 发出时取消订阅
  14. );
  15. pollOmni() {
  16. 返回 this.http.post<any>('http://127.0.0.1:8044/api', { call: 'getInfo' });
  17. }
  18. ngOnInit() {
  19. this.poll.subscribe();
  20. }
  21. ngOnDestroy() {
  22. this.ngUnsubscribe.next(); // 发出值以取消进行中的订阅
  23. this.ngUnsubscribe.complete(); // 完成主体
  24. }
  25. }
英文:

I have a code that calls http post and recieves the observable notification when data is recieved .
I wish to know how to get the json respose from the server in the following function since when I try to print it it only shows the observable and not the actual json returned from the server:

  1. display = response =&gt; {
  2. console.log(response);
  3. };

The complete code is :

  1. import { Component, OnInit, OnDestroy } from &#39;@angular/core&#39;;
  2. import { HttpClient } from &#39;@angular/common/http&#39;;
  3. import { of, Subject } from &#39;rxjs&#39;;
  4. import { delay, tap, mergeMap, repeat, takeUntil } from &#39;rxjs/operators&#39;;
  5. @Component({
  6. selector: &#39;app-root&#39;,
  7. templateUrl: &#39;./app.component.html&#39;,
  8. styleUrls: [&#39;./app.component.css&#39;]
  9. })
  10. export class AppComponent implements OnInit, OnDestroy {
  11. private ngUnsubscribe = new Subject&lt;void&gt;(); // Subject for unsubscribing
  12. constructor(private http: HttpClient) { }
  13. callOmni = () =&gt; of(this.pollOmni()).pipe();
  14. display = response =&gt; {
  15. console.log(response);
  16. };
  17. poll = of({}).pipe(
  18. mergeMap(_ =&gt; this.callOmni()),
  19. tap(this.display),
  20. delay(10),
  21. repeat(),
  22. takeUntil(this.ngUnsubscribe) // Unsubscribe when ngUnsubscribe emits
  23. );
  24. pollOmni() {
  25. return this.http.post&lt;any&gt;(&#39;http://127.0.0.1:8044/api&#39;, { call: &#39;getInfo&#39; });
  26. }
  27. ngOnInit() {
  28. this.poll.subscribe();
  29. }
  30. ngOnDestroy() {
  31. this.ngUnsubscribe.next(); // Emit value to unsubscribe from ongoing subscriptions
  32. this.ngUnsubscribe.complete(); // Complete the subject
  33. }
  34. }

答案1

得分: 0

错误在于 callOmni = () =&gt; of(this.pollOmni())
this.poolOmni 已经返回一个可观察对象,而您在其中再次使用 of 包装了一个可观察对象。
如果直接调用 poolOmni,代码应该能正常工作。

英文:

The error is in callOmni = () =&gt; of(this.pollOmni()).
this.poolOmni already returns an observable, which you are wrapping in another observable with of.
If you call directly poolOmni the code should work fine

huangapple
  • 本文由 发表于 2023年7月3日 22:28:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605702.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定