0

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;
Divakar R
  • 81
  • 5
  • 12
  • Possible duplicate of [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 Nov 28 '18 at 08:38
  • Are you sending the fetch request to a backend express file? If so please display that. – djamaile Nov 28 '18 at 09:21
  • no. i am not sending fetch request to backend express – Divakar R Nov 28 '18 at 10:09

2 Answers2

0

You may need to whitelist your port and local address in JIRA.

SBimochan
  • 474
  • 4
  • 13
-2

Your browser security might be block your request. Try to open browser in not security mode.

on macos:

open -a Google\ Chrome --args --disable-web-security --user-data-dir 
itidsu
  • 54
  • 10