I am working on react.js project where i have to fetch the data from JIRA rest api and display it using react.js . API works fine when i am accessing it directly from the browser but when i try to fetch using react.js its causing the below error.
Access to fetch at 'API' 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.
Note:
I have tried mode : " no-cors" and even installing npm cors lib but they are not solving the problem.
React.js code
import React, { Component } from 'react';
class App extends Component {
constructor(props)
{
super(props);
this.state = {
items: [],
isLoaded: false,
}
}
componentDidMount()
{
fetch('API which i cant display but works 100% fine . please ignore')
.then(res => res.json())
.then(json => {
this.setState({
isLoaded: true,
items: json,
})
})
.catch(e => { console.log("error: ",e) });
}
render()
{
var { isLoaded, items } = this.state;
console.log("Hello ", items);
if (!isLoaded)
{
return <div>Loading..</div>
}
else {
return (
<div className="App">
Id : item.id
<ul>
{items.map(item => (
<li key={item.id}>
Name: {item.name}
Email: {item.email}
</li>
))};
</ul>
</div>
);
}
}
}
export default App;