我正在为Angular使用ng2-pdf-viewer库,我正在尝试从基本URL显示PDF。 问题是我收到以下错误:
XMLHttpRequest无法加载 https://www.bbv.ch/images/bbv/pdf/Publikationen/BBV16Poster_Angular2.pdf。 请求中不存在“Access-Control-Allow-Origin”标头 资源。因此不允许来源“http://localhost:54800” 访问。
这是我的代码:
.ts:
- (void)viewDidLoad {
BaseString = @"http://api.map.baidu.com/place/v2/suggestion?query=";
ak = @"56dIEtBAp1CU7u8ZMcq8DyUH2mVsn38x"; mcode = @"com.baidu.Baidu-Map-Demo";
regionkey = @"中国";
PathString = @"http://api.map.baidu.com/direction/v2/transit?origin=";
self .mapView .userTrackingMode = BMKUserTrackingModeFollow;
// 2. Set the map type self.mapView.mapType = BMKMapTypeStandard;
// 3. Set Agent self.mapView.delegate = self;
[super viewDidLoad];
mapView.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height);
mapView.delegate = self; anotation = [[BMKPointAnnotation alloc]init];
destination = [[BMKPointAnnotation alloc]init];
PathUrl = [[NSURL alloc]init];
finalPathArray = [[NSMutableArray alloc]init];
session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
downloadURL = [[NSURL alloc]init];
path = [[BMKPolyline alloc]init];
flag = 0;
}
-(void)GetSuggestion: (NSString *)query {
NSString *stringUrl = [NSString stringWithFormat:@"%@%@&page_size=10&page_num=0&scope=1®ion=%@&output=json&ak=%@&mcode=%@",BaseString,query,regionkey,ak,mcode]; stringUrl = [stringUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
downloadURL = [NSURL URLWithString:stringUrl];
if (downloadURL != nil) {
if (DownloadTask != nil) {
[DownloadTask suspend];
}
DownloadTask = [session dataTaskWithURL:downloadURL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *AutocompleteData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
resultArray = AutocompleteData[@"result"];
tbl_result.hidden = NO;
[tbl_result reloadData];
}];
[DownloadTask resume];
}
}
.html:
{ Name: "Angular2 tuto", Description: "An amazing Angular2 pdf !", Url: {url: "https://www.bbv.ch/images/bbv/pdf/Publikationen/BBV16Poster_Angular2.pdf", withCredentials: true} }
我尝试使用和不使用“withCredentials”属性 - 没有任何改变..
如何从angular2中的链接显示任何pdf? (虽然在基本的iframe工作......)
答案 0 :(得分:-1)
导入这些: -
import { DomSanitizer, SafeResourceUrl, SafeUrl } from '@angular/platform-browser';
定义变量: -
Url : SafeUrl;
在像这样的构造函数中使用它 -
constructor(private sanitizer: DomSanitizer) {}
这样的特定网址 -
this.Url = this.sanitizer.bypassSecurityTrustResourceUrl("https://www.bbv.ch/images/bbv/pdf/Publikationen/BBV16Poster_Angular2.pdf");