Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

CORS policy issue in React and fetch

Im trying to fetch data from external api using fetch in react typescript component and having the error:

Access to fetch at ‘https://api.airtable.com/{my_data_base}’ from origin ‘http://localhost:3000’ has been blocked by CORS policy: Request header field authentication is not allowed by Access-Control-Allow-Headers in preflight response.

My React tsx component is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import { FC, useEffect } from 'react';

const Collection: FC = () => {
    useEffect(() => {
        fetch('https://api.airtable.com/v0/my_data_base', {
            headers: {
                mode: 'no-cors',
                Authentication: 'Bearer my_token',
            },
        })
            .then((resp) => resp.json())
            .then((data) => {
                console.log(data);
            });
    }, []);

    return <div>Collection Page Component</div>;
};

export default Collection

The same logic works fine with other apis, that don’t require authorisation, like for example https://jsonplaceholder.typicode.com/todos/1

Any idea how to fix it?
I’ve tried even the most stupid solution which is browser extensions but non of them worked.

>Solution :

First, the mode property is not part of the headers object but it belongs to the entier options object just like herders, second you don’t need it.
Also the property for passing tokens is Authorization not Authentication.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading