I use Axios to sending request to my NodeJS server. This is my request:
let url = 'http://example.com:1337/api/'
let config = {
headers: {
'Content-Type': 'application/json'
}
}
settings = (data) => {
return axios.post(url + 'settings', JSON.stringify(data), config)
.then( res => res.data)
.catch(e => console.log(e))
}
In NodeJS/ExpressJS server:
const express = require('express')
const App = express()
let bodyParser = require('body-parser')
// ... Mongoose and etc.
var cors = require('cors')
App.use(cors())
App.options('*', cors());
App.use("/data", express.static(__dirname + '/data'));
App.use(bodyParser.urlencoded({
extended: true
}));
App.use(bodyParser.json())
App.use('/api', require('./routes/Users'))
App.listen(1337)
But request to http://example.com:1337/api/settings
returns (firefox):
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://example.com:1337/api/settings. (Reason: CORS request did not succeed)
Thank you for your solutions.
2
Answers
Try this: