0

this is my code:

const request = require('request');

  const options = {
      url: 'https://api.github.com/',
      headers: {
        'User-Agent': 'request',
        'Access-Control-Allow-Origin':'*',
      },
    };
    request(options, (error, response, body) => {
      if(error) {
        console.log("Error: " + error);
      }
      else{
        console.log("Status code: " + response.statusCode);
      }
    });

and the result is this:

Access to fetch at 'https://api.github.com/' from origin 'http://localhost:3000' has been blocked by CORS policy: Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response.

if i delete the -'Access-Control-Allow-Origin':'*' - it's work but just in this website

how can i fix it?

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
  • Don't you need to specify a path such as https://api.github.com/organizations? – Firmino Changani Apr 04 '21 at 15:44
  • This doesn't make sense to me. CORS policy is something a server sets, not a client. What you have shown here is client code. Plus, the error method refers to `fetch` which is a browser thing, yet you're question appears to be using nodejs and the request library which have nothing to do with the `fetch()` library in the browser. Totally confused. So, it appears that the error message you show does NOT come from the code you show. You will need to show us the code for the WHOLE problem. – jfriend00 Apr 04 '21 at 16:15
  • Firmino Changani - no, it's need to be for dynamic websites. – Aviel Palgi Apr 04 '21 at 16:44
  • jfriend00 - `const request = require('request'); var fs = require('fs'); const cheerio = require('cheerio') function App() { useEffect(() => { const options = { url: 'https://api.github.com/', headers: { 'User-Agent': 'request', 'Access-Control-Allow-Origin':'*', }, }; request(options, (error, response, body) => { if(error) { console.log("Error: " + error); } else{ console.log("Status code: " + response.statusCode); } }); }, [])` – Aviel Palgi Apr 04 '21 at 16:47
  • that's all the code. i just want to make a dynamic web crawler – Aviel Palgi Apr 04 '21 at 16:47

0 Answers0