I'm calling Drupal API from React JS and getting the following error :
Failed to load http://l-and-d.dd:8083/node?_format=json: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
I have already allowed all the requests on my Drupal server and I'm still getting this error. I think the error is on the client side. I have also added the headers on webpack.config.js file Here is webpack.config.js file
const path = require('path')
module.exports = {
entry: './src/app.js',
output: {
path: path.join(__dirname, 'public'),
filename: 'bundle.js'
},
module: {
rules: [{
loader: 'babel-loader',
test: /\.js$/,
exclude: /node_modules/
}, {
test: /\.scss$/,
use: [
'style-loader',
'css-loader',
'sass-loader'
]
},
{
test: /\.(eot|svg|woff|woff2|ttf|png|svg|jpg|jpeg|bmp|gif|pdf)$/,
use: [
'file-loader'
]}
]
},
devtool: 'cheap-module-eval-source-map',
devServer: {
contentBase: path.join(__dirname, 'public'),
historyApiFallback: true,
headers: {
'Access-Control-Allow-Origin' : '*'
}
}
}
I have also tried adding the custom headers in my code but that too didn't work, here is the API calling code :
axios({
method: 'POST',
url: 'http://l-and-d.dd:8083/node?_format=json',
data: node,
headers: {'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Origin': '*',
'Access-Control-Expose-Headers':'*',
'Content-Type':'application/json',
'Accept':'application/json',
'X-Custom-Header': '*' }
}).then((res) => {
console.log(res)
}).catch((err) => {
console.log(err)
})