简体   繁体   English

使用 Angular 8 中的 http.get() 调用在 iframe 中显示报告

[英]Show report in iframe with http.get() call in Angular 8

I'm trying to load a report with an http.get() call and show it in an iframe using angular 8.我正在尝试使用 http.get() 调用加载报告,并使用 angular 8 在 iframe 中显示它。

Here is the API response.这是 API 响应。 在此处输入图片说明

Component.ts组件.ts

    export class LogsComponent implements OnInit {
  logsData;
  logs;

  displayString

  constructor( private httpService: HttpService, private loaderService: LoaderService,
    private domSanitizer: DomSanitizer) { }

  ngOnInit() {
    this.httpService.getLogs().subscribe(data => {
      this.logsData = data;
      this.displayString = this.domSanitizer.bypassSecurityTrustHtml(this.logsData);
    })

  }

}

component.html组件.html

<iframe [srcdoc]="displayString"></iframe>

Getting this error.得到这个错误。 在此处输入图片说明

It looks like your HttpService is getting a string back from the API that cannot be deserialized into a JSON object.看起来您的 HttpService 正在从 API 获取一个无法反序列化为 JSON 对象的字符串。 You can return put the string response into an object that has a property to store that string.您可以将字符串响应返回到具有存储该字符串的属性的对象中。 On the component you can unwrap that JSON object and pass it to the component.在组件上,您可以解开该 JSON 对象并将其传递给组件。 ** One IMPORTANT thing to note is that Angular will see the string as insecure and you will need to sanitize the string by call the DomSanitizert.bypassSecurityTrustHtml and pass in the string to that method. ** 需要注意的一件重要事情是 Angular 会将字符串视为不安全,您需要通过调用 DomSanitizert.bypassSecurityTrustHtml 来清理字符串并将字符串传递给该方法。 Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM