Angular - 使用脚本在iframe上注入自定义标头

时间:2018-06-04 09:59:08

标签: angular iframe header httpclient

Angular 2 - Inject custom headers on iframe的答案是文件。如何使用带有工作脚本和HttpClient的完整站点来完成这项工作?

我来到that,但看起来它只是加载了没有功能的普通html文本。

我的代码:

private buildContentViewURL(osid: number, contentViewType: string) {
    if (contentViewType === "indexdata") {
      this.searchService.loadiFrame("/detailsviewer/index.html?osid=" + osid + "lang=" + "DE")
        .subscribe(blob => this.url = this.sanitizer.bypassSecurityTrustResourceUrl(blob));
    } else {
      this.searchService.loadiFrame("/osdocumentviewer/app/viewer/" + osid)
        .subscribe(blob => this.url = this.sanitizer.bypassSecurityTrustResourceUrl(blob));
    }
  }

public loadiFrame(url: string): Observable<any> {
    const authentification = "basic "
      + btoa(`${AppConfig.settings.connection.User}:${AppConfig.settings.connection.Password}`);

    return new Observable((observer: Subscriber<any>) => {
      let objectUrl: string = null;

      this.http
        .get(url, {
          headers: {
            "Authorization": authentification,
            "Access-Control-Allow-Origin": "*"
          }, responseType: "blob"
        })
        .subscribe(m => {
          objectUrl = URL.createObjectURL(m);
          observer.next(objectUrl);
        });

      return () => {
        if (objectUrl) {
          URL.revokeObjectURL(objectUrl);
          objectUrl = null;
        }
      };
    });
  }

我需要添加自定义标头以自动进行身份验证。

0 个答案:

没有答案