I have a simple Website, which needs to access data by sending a GET request to my NodeJS server. Everything worked on localhost, but now i am moving onto a server an i always get a
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3001/data/update. (Reason: CORS request did not succeed). Status code: (null).
Error!
I added the required CORS in my server.js:
const express = require('express');
const cors = require('cors');
const db = require('./data/database');
const app = express();
// allow CORS requests
app.use(cors());
app.use(express.json());
// use routes
app.use('/data', require('./routes/data'));
app.use('/login', require('./routes/login'));
// listen at port 3001
app.listen(3001, () => console.log(`
Server: Running at port 3001
`));
My request is looking like this using JQuery:
data = (await $.get('http://localhost:3001/data'));