No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs,
It was really a bit mistake which I was doing but now resolved,
I was trying to send a SMS from Twilio API
//
const app = express();
const express = require('express');
app.use(cors()); <<<<<
//
if you just comment on this above line you get the same error mentioned below, I have set upped react server/website on 3000 port and express server on 4000 port. so the website sends a post request running on 3000 port to the server on port 4000 mechanisms called cross-origin request.
//
Access to fetch at 'http://localhost:4000/api/send-sms' 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.
//
The CORS mechanism supports secure cross-origin requests and data transfers between browsers and servers.
modern browsers use CORS in APIs such as XMLHttpRequest or Fetch to mitigate the risks of cross-origin HTTP requests.
For security reasons, browsers restrict cross-origin HTTP requests initiated from scripts.
For example, XMLHttpRequest and the Fetch API follow the same-origin policy. This means that a web application using those APIs can only request resources from the same origin the application was loaded from unless the response from other origins includes the right CORS headers.
So I have also answered on StackOverflow I have mentioned code snippets also if you found it useful just do upvotes 🔼 or any suggestions or improvements are most welcome.
References: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
Comments
Post a Comment