Sunday, September 25, 2022

Access-Control-Allow-Origin not present: CORS policy

Access to fetch at 'http://127.0.0.1:3001/api/v1/posts' 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.

const posts = () => {

fetch('http://127.0.0.1:3001/api/v1/posts')
.then((r) => r.json()
.then((data) => ({ status: r.status, body: data })))
.then((obj) => console.log(obj));
};

Use 'api/v1/posts' instead 'http://127.0.0.1:3001/api/v1/posts'

If you want to enable CORS for all the request  => npm i cors --save-dev

const cors = require('cors');
app.use(cors())

https://stackabuse.com/handling-cors-with-node-js/

fetch(url, {
method: 'GET',
mode: 'no-cors', // set this mode
dataType: 'json',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
})

fetch_url = https://site.tech/api/tech

ping site.tech // get IP Adress and put it instred url

fetch_url = https://0.0.0.0/api/tech

No comments:

Post a Comment