-2

I'm trying to get data through HTTP get request but I get the CORS error. I suppose the error is due to I don't use an "official" API. However, I am really curious whether it is possible to do web query of specific websites such as google or job-search website without using API via angular.

You can see my code as follows:

import { HttpClient, HttpHeaders } from "@angular/common/http";
import { Component } from "@angular/core";

@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.scss"],
})
export class AppComponent {
  title = "job-search";
  url = "http://de.indeed.com/Jobs?as_and=Sprachlehrer";

  constructor(private _http: HttpClient) {}

  //"http://de.indeed.com/Jobs?as_and=Lehrer"
  ngOnInit() {
    let headersOpt = new HttpHeaders();
    headersOpt.set("Content-Type", "application/json");
    headersOpt.set("Access-Control-Allow-Origin", "*");
    this._http
      .get(this.url, {
        headers: headersOpt,
      })
      .subscribe((res) => {
        console.log(res);
      });
  }
}

I don't have a "real" HTML file here because I just want to get the query result.

Thank you for your solution.

Best regards I

Kai-Chun Lin
  • 185
  • 1
  • 2
  • 10
  • Does this answer your question? [XMLHttpRequest cannot load XXX No 'Access-Control-Allow-Origin' header](https://stackoverflow.com/questions/35553500/xmlhttprequest-cannot-load-xxx-no-access-control-allow-origin-header) – Quentin Mar 09 '21 at 09:53
  • Kind of because I already know the reason that causes this CORS error. However, thank you very much! – Kai-Chun Lin Mar 09 '21 at 18:23

1 Answers1

1

The Access-Control-Allow-Origin header must be set in the response of the server you are trying to access, not in your client. Since it is not your server, you will get the CORS error.

Most of the times an API requires an API key. Without this key, you will not be able to call the API.

To answer your question, you could write (or use) a website crawler, which extracts data from the HTML of the page. This way you don't have to call the actual API.

You may read more about CORS and the Same-origin policy here.

  • Do you mean I should put the header with Access-Control-Allow-Origin within get(), don't you? I know the reason cuasues this CORS error because I don't get a API key. I've tried to reach indeed but still get any reply tells me how to get key. Thanl you for your answer. – Kai-Chun Lin Mar 09 '21 at 18:22