Angular http.get() url as raw HTML
Asked Answered
S

2

6

I'm creating a site for an assignment, where I want to dynamically load some data. Problem is, that data just comes from a website, no API or anything. Is there some way that I can use http.get in angular to pull the whole site as raw HTML, which I can then parse for info?

Thanks

Sander answered 10/7, 2019 at 11:11 Comment(0)
H
11

You can set the responseType to "text" to fetch the response as a string.

this.httpClient.get(url, {responseType: "text"})

See overload method #3:

https://angular.io/api/common/http/HttpClient#get

Note: Cross domain requests for GET are subject to CORS

Hominy answered 10/7, 2019 at 11:18 Comment(1)
Thanks, got it working with that plus using a cors proxySander
S
0

Working Example

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 }}
Slay answered 10/7, 2019 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.