I'm trying to get a web page with following code so that I can scrape its data, but I keep getting the error: XMLHttpRequest cannot load https://websiteURL.com. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. I have read that I need to set the 'Access-Control-Allow-Origin' name in the header, I have tried using a wildcard '*' as well as my localhost, nothing works.
Here is my typescript code:
import { Component, OnInit } from '@angular/core';
import { Http, Response, RequestOptionsArgs, Headers } from '@angular/http';
....
constructor(private http: Http) { }
....
doScrape() {
var header : Headers = new Headers();
header.append('Access-Control-Allow-Origin', 'http://localhost:4200');
var args : RequestOptionsArgs = {
method: "GET",
headers: header
}
console.log('Getting html...');
this.http.get(this.b, args).subscribe(res => {
console.log(res);
this.htmlString = res.text();
})
}
Why isn't this getting the job done?