0

I am trying to make an API call with bearer token to get the data. But there an error Access to fetch at 'https://MYAPI..' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: 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. Also, my token is null. I try to get from localStorage.local storage token example Here is my token. Can anyone suggest to me where is my problem? Thanks

const [user, setUsers] = React.useState([]);
 var token = localStorage.getItem("accessToken");
    React.useEffect(()=>{
   fetch(URLS.GetAllUsers,{
    method: "GET",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + token,
      
        }})
    .then(res=> res.json())
    .then((response) => {
  setUsers(response.displayName)
    })
    .catch((error) => console.log(error));
  });
Nibble
  • 17
  • 1
  • 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) – im_baby May 16 '21 at 14:28

1 Answers1

0

I think you have to enable CORS inside Server.

WebEXP0528
  • 561
  • 3
  • 9