2

App.js :

import React from 'react';
import './App.css';

class App extends React.Component {

  state = {
    character : {}
  };

  componentDidMount() {
    fetch("https://jobs.github.com/positions.json?description=basf")
      .then(data => console.log(data));
    console.log("Hello World");
  }

  render() {
    return (
      <div className="App">
        <h2>Hello World</h2>
      </div>
    );
  }
}

export default App;

Error Message -

Access to fetch at 'https://jobs.github.com/positions.json?description=basf' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Failed to Fetch

Can you please provide me how to get past this issue and get required data. I am using Github Jobs API and its showing this message.

onlyphantom
  • 8,606
  • 4
  • 44
  • 58
  • Does this answer your question? [No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API](https://stackoverflow.com/questions/43871637/no-access-control-allow-origin-header-is-present-on-the-requested-resource-whe) – Edric Dec 30 '19 at 06:53
  • Dont you nead to pass credentials in order to acess that API ? – Stefan Avramovic Dec 30 '19 at 07:17

4 Answers4

1

Here are several ways you can get it done

ONE

import React from 'react';
import './App.css';

class App extends React.Component {

  state = {
    character : {}
  };

  componentDidMount() {
    fetch("https://jobs.github.com/positions.json?description=basf",{
                headers: {
                  'Content-Type': 'application/json',
                  'Access-Control-Allow-Origin': '*'
                })
      .then(data => console.log(data));
    console.log("Hello World");
  }

  render() {
    return (
      <div className="App">
        <h2>Hello World</h2>
      </div>
    );
  }
}

export default App;

TWO

Setting the mode parameter to no-cors.

fetch("https://jobs.github.com/positions.json?description=basf", {
      mode: "no-cors" // 'cors' by default
    }).then(data => console.log(data));
Lewa Bammy Stephen
  • 401
  • 1
  • 5
  • 14
1

Try this

you can solve this from frontend side if you are facing cors issue. using-

https://cors-anywhere.herokuapp.com/{type_your_url_here}

  componentDidMount(){
    fetch('https://cors-anywhere.herokuapp.com/https://jobs.github.com/positions.json?description=basf'
    )
    .then(data => data.json())
    .then(data=>console.log(data))
  }

Working Demo

akhtarvahid
  • 9,445
  • 2
  • 26
  • 29
0

By using the create-react-app proxy feature you can solve this problem.

Add proxy property to your package.json:

{
  // ...
  proxy: "https://jobs.github.com"
}

And then call the fetch function:

fetch("http://localhost:300/positions.json?description=basf")
      .then(data => console.log(data));
Ali Torki
  • 1,929
  • 16
  • 26
-1

You need to use the no-cors header

 fetch("https://jobs.github.com/positions.json?description=basf", {
      mode: "no-cors" // 'cors' by default
    }).then(data => console.log(data));

look in this codesandbox

I hope this answer helped you :)

Dor Lugasi-Gal
  • 1,430
  • 13
  • 35