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

No 'Access-Control-Allow-Origin' header when fetch data with REACT from API

I have an urgent problem I hope you can help me as soon as possible.

I have a test to get products from a company API.

They told me ((( CORS allowed origins for local development is "http://localhost:1229" ))).

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

So I set up proxy in package.json like this:

"proxy": "http://localhost:1229",

And my request is :

const apiLink = 'https://example.com/api/checkout/products/';

    fetch(apiLink, {
        method: 'POST'
    }).then(returnedData => {
        console.log(returnedData)
    }).catch(error => {
        console.log(error)
    });

But I still get this error

Access to fetch at 'https://...' 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

please how can i solve this problem.

>Solution :

CORS allow get content from origin http://localhost:1229
so, try run development server on http://localhost:1229

using command
on linux
PORT=1229 react-scripts start
on windows
set PORT=1229 && react-scripts start

or change package.json
"start": "PORT=1229 react-scripts start"
"start": "set PORT=1229 && react-scripts start"

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