ng2-pdf-viewer访问控制允许来源 [英] ng2-pdf-viewer Access-Control-Allow-Origin
问题描述
我正在使用Angular的ng2-pdf-viewer库,并且尝试显示基本网址中的PDF. 问题是我得到以下错误:
I am using the ng2-pdf-viewer library for Angular and I am trying to display a PDF from a basic url. the problem is I get the following error :
XMLHttpRequest无法加载 https://www.bbv.ch/images/bbv/pdf/Publikationen /BBV16Poster_Angular2.pdf . 请求中不存在"Access-Control-Allow-Origin"标头 资源.因此,不允许使用来源" http://localhost:54800 " 访问.
XMLHttpRequest cannot load https://www.bbv.ch/images/bbv/pdf/Publikationen/BBV16Poster_Angular2.pdf. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:54800' is therefore not allowed access.
这是我的代码:
.ts:
{ Name: "Angular2 tuto", Description: "An amazing Angular2 pdf !", Url: {url: "https://www.bbv.ch/images/bbv/pdf/Publikationen/BBV16Poster_Angular2.pdf", withCredentials: true} }
.html:
<pdf-viewer [src]="document.Url" [page]="1" [original-size]="false" style="display: block;"></pdf-viewer>
我尝试使用和不使用"withCredentials"属性-均未更改.
I tried with and without "withCredentials" property - nothing changed..
如何显示angular2链接中的任何pdf? (虽然在基本的iframe中工作过...)
How can I show any pdf from a link in angular2 ? (worked in a basic iframe though...)
推荐答案
导入这些:-
import { DomSanitizer, SafeResourceUrl, SafeUrl } from '@angular/platform-browser';
定义变量:-
Url : SafeUrl;
在像这样的构造函数中使用它-
Use it in constructor like this-
constructor(private sanitizer: DomSanitizer) {}
这样的指定URL-
this.Url = this.sanitizer.bypassSecurityTrustResourceUrl("https://www.bbv.ch/images/bbv/pdf/Publikationen/BBV16Poster_Angular2.pdf");
这篇关于ng2-pdf-viewer访问控制允许来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!