Angular http.get() url 原始 HTML
Angular http.get() url as raw HTML
我正在为一项任务创建一个站点,我想在其中动态加载一些数据。问题是,该数据仅来自网站,没有 API 或其他任何内容。有什么方法可以让我在 angular 中使用 http.get 将整个站点拉为原始 HTML,然后我可以解析它以获取信息?
谢谢
您可以将 responseType
设置为 "text"
以获取字符串形式的响应。
this.httpClient.get(url, {responseType: "text"})
参见重载方法#3:
https://angular.io/api/common/http/HttpClient#get
Note: Cross domain requests for GET are subject to CORS
service.ts
getRawData(): Observable<any> {
const api = 'https://www.w3.org/TR/PNG/iso_8859-1.txt';
return this.httpClient.get(api,{ responseType: 'text' });
}
component.ts
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { AppService } from './app.service';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
name = 'Angular 5';
rawlist: any;
constructor(private appService: AppService) {}
ngOnInit() {}
getRawData() {
this.appService
.getRawData()
.subscribe(
data => this.rawlist=data,
error => console.log(error)
);
}
}
.html
<button (click)="getRawData()">get raw data </button>
{{rawlist }}
我正在为一项任务创建一个站点,我想在其中动态加载一些数据。问题是,该数据仅来自网站,没有 API 或其他任何内容。有什么方法可以让我在 angular 中使用 http.get 将整个站点拉为原始 HTML,然后我可以解析它以获取信息?
谢谢
您可以将 responseType
设置为 "text"
以获取字符串形式的响应。
this.httpClient.get(url, {responseType: "text"})
参见重载方法#3:
https://angular.io/api/common/http/HttpClient#get
Note: Cross domain requests for GET are subject to CORS
service.ts
getRawData(): Observable<any> {
const api = 'https://www.w3.org/TR/PNG/iso_8859-1.txt';
return this.httpClient.get(api,{ responseType: 'text' });
}
component.ts
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { AppService } from './app.service';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
name = 'Angular 5';
rawlist: any;
constructor(private appService: AppService) {}
ngOnInit() {}
getRawData() {
this.appService
.getRawData()
.subscribe(
data => this.rawlist=data,
error => console.log(error)
);
}
}
.html
<button (click)="getRawData()">get raw data </button>
{{rawlist }}