解析Angular HTTP响应并处理JSON。

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

Parse Angular Http Reponse And Handle Json

问题

显示 = 响应 => {
    console.log(响应);
};
导入 { Component, OnInit, OnDestroy }  '@angular/core';
导入 { HttpClient }  '@angular/common/http';
导入 { of, Subject }  'rxjs';
导入 { delay, tap, mergeMap, repeat, takeUntil }  'rxjs/operators';
export class AppComponent 实现 OnInit, OnDestroy {
    私有 ngUnsubscribe =  主体<void>(); // 用于取消订阅的主体

    构造函数(private http: HttpClient) { }

    callOmni = () => of(this.pollOmni()).pipe();

    显示 = 响应 => {
        console.log(响应);
    };

    轮询 = of({}).pipe(
        mergeMap(_ => this.callOmni()),
        tap(this.display),
        延迟(10),
        重复(),
        takeUntil(this.ngUnsubscribe) // 当 ngUnsubscribe 发出时取消订阅
    );

    pollOmni() {
        返回 this.http.post<any>('http://127.0.0.1:8044/api', { call: 'getInfo' });
    }

    ngOnInit() {
        this.poll.subscribe();
    }

    ngOnDestroy() {
        this.ngUnsubscribe.next(); // 发出值以取消进行中的订阅
        this.ngUnsubscribe.complete(); // 完成主体
    }
}
英文:

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:

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

The complete code is :

import { Component, OnInit, OnDestroy } from &#39;@angular/core&#39;;
import { HttpClient } from &#39;@angular/common/http&#39;;
import { of, Subject } from &#39;rxjs&#39;;
import { delay, tap, mergeMap, repeat, takeUntil } from &#39;rxjs/operators&#39;;

@Component({
  selector: &#39;app-root&#39;,
  templateUrl: &#39;./app.component.html&#39;,
  styleUrls: [&#39;./app.component.css&#39;]
})
export class AppComponent implements OnInit, OnDestroy {
  private ngUnsubscribe = new Subject&lt;void&gt;(); // Subject for unsubscribing

  constructor(private http: HttpClient) { }

  callOmni = () =&gt; of(this.pollOmni()).pipe();

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

  poll = of({}).pipe(
    mergeMap(_ =&gt; this.callOmni()),
    tap(this.display),
    delay(10),
    repeat(),
    takeUntil(this.ngUnsubscribe) // Unsubscribe when ngUnsubscribe emits
  );

  pollOmni() {
    return this.http.post&lt;any&gt;(&#39;http://127.0.0.1:8044/api&#39;, { call: &#39;getInfo&#39; });
  }

  ngOnInit() {
    this.poll.subscribe();
  }

  ngOnDestroy() {
    this.ngUnsubscribe.next(); // Emit value to unsubscribe from ongoing subscriptions
    this.ngUnsubscribe.complete(); // Complete the subject
  }
}

答案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:

确定