【发布时间】:2019-02-04 23:32:38
【问题描述】:
我从 React JS 调用 Drupal API 并收到以下错误:
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.
我已经允许我的 Drupal 服务器上的所有请求,但我仍然收到此错误。 我认为错误出在客户端。 我还在 webpack.config.js 文件中添加了标题 这是 webpack.config.js 文件
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' : '*'
}
}
}
我也尝试在我的代码中添加自定义标头,但这也不起作用, 这是API调用代码:
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)
})
【问题讨论】:
标签: reactjs cors webpack-dev-server drupal-8