Access to fetch at from origin 'http://localhost:3000' has been blocked by CORS policy
Solution 1
try using ''no-cors' mode.
fetch('http://localhost:8080/example', {
mode: 'no-cors',
method: "post",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(ob)
})
Solution 2
Your server should respond with a response that looks like below
Access-Control-Allow-Origin: https://localhost:3000
Untill you are able to configure that as a workaround you can install the below chrome extension to resume your testing
https://chrome.google.com/webstore/detail/who-cors/hnlimanpjeeomjnpdglldcnpkppmndbp?hl=en-GB
But the above is only a workaround to continue development
I would suggest you read this article for understanding CORS https://javascript.info/fetch-crossorigin
Solution 3
I resolved the issue by installing the cors
node module and adding this on the requested server
const cors = require("cors");
app.use(cors());
Solution 4
You must install cors.
npm install cors
inside the main scripts index.js or app.js
const cors = require("cors");
app.use(cors());
Solution 5
For browser CORS is enabled by default and you need to tell the Browser it's ok for send a request to server that not served your client-side app ( static files ).
if you use RestFul API with node and express add this middleware to your file
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*")
})
Related videos on Youtube
dcielak
Updated on July 09, 2022Comments
-
dcielak 5 months
Adding to the database shows the error. what should I do?
Access to fetch at 'http:xxx' 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.
my function:
addItem = (e) => { e.preventDefault(); const ob = { X: 53.0331258, Y: 18.7155611, } fetch("http://xxx", { method: "post", headers: { "Content-Type": "application/json" }, body: JSON.stringify(ob) }) .then(res => res.json()) .then(res => { console.log(res); }) }
-
happyZZR1400 over 2 yearswhen you trying to make request from different domain (xxx in your case)the browser not allow to send such request. if xxx is your server - you can configure it to send the headers that allows cros domain request
-
Jonathan over 2 yearsIt actually tells you what the issue is. See stackoverflow.com/a/10636765/12989672
-
Heretic Monkey over 2 yearsDoes this answer your question? How does Access-Control-Allow-Origin header work?
-
Saurav Seth over 2 yearsI thought localhost was now granted an exception from CORS restrictions on modern browsers including Chrome. You are pulling in your HTML directly from a web server and not from a local source file are you?
-
-
William over 1 yearapp is not defined
-
ABDERRAHMANE OUALI over 1 yearyou should first import express at the top by;
const express = require("express");
thenconst app = express();
-
K.K Desgins 10 monthsWhat would be the point of a security system with a button saying "disable me" on the outside? See Trying to use fetch and pass in mode: no-cors —
{ mode: 'no-cors' }
makes things worse. -
White Rabbit 7 monthsHello Habibur Rahman =) Welcome to StackOverfollow. Im happy to assist you with any queries you may have regarding using this Platform. One thing thats allways a great benifit to others is to add comments to your code sameplates to allow you to reach a loarger audience. That being said, try and formate your code blocks with the ` symbole i.e using it in one line just your code between the backticks
Hollo World!
if you need a larger code block you can surround your block off code with 3 backticks and the begening of the block snd 3 at the end i.e ``` If (this == that) { // computed value }