2

How can I fix the access to XMLHttpRequest at... from origin 'null' has been blocked by CORS policy: ... with NO access to the server side.

I am working on a project, and part of it requires me to scrape for some information off of a specific page.

Now, this URL can only be accessed if I am connected to the network of my workplace.

The following is the code I am using in order to scrape:

  let axios = require('axios');
  let cheerio = require('cheerio');

  axios.get("---the url that I want---")
    .then((response) => {
        if(response.status === 200) {
          // sucsessfull call to get
          const html = response.data;
          const $ = cheerio.load(html);

          const jobTable = $('#tbl_JobInfo');
          const table = jobTable.find('table');
          console.log(table.text());

    }
    }, (error) => console.log(err) );

However, when using this I get the following error:

Access to XMLHttpRequest at '---the url that I want---' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I've done a lot of searching online and understand this is a CORS issue, but I have no access to change headers on server side.

Is there any way I can work around and gain access to information on this page, as I am connected to the network of the workplace?

Thanks

Khokhar
  • 149
  • 2
  • 9
  • 1
    You could proxy the call through your own server, meaning you call a URL in your server, that calls the URL from the other page and then return the result. – solarc May 08 '19 at 13:30

1 Answers1

1

You can create a proxy server and place it on the network of your workplace. Your client will then communicate with this proxy server rather than directly with the server you want to scrape from.

The proxy server is just a simple web server which accepts the GET request from your client, fetches the HTML from the URL and sends it back to the client.

Sync
  • 3,571
  • 23
  • 30