[英]Express server and Axios CORS Preflight response is not successful
I have the following get action using react.js ( running on port 3000) 我有以下使用react.js的get操作(在端口3000上运行)
var config = {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
// 'Accept': 'application/json',
// 'Content-Type': 'application/json',
}
};
axios.get(api_url + '/schools/countries/' + country, config)
.catch(err => {
alert('There was an error trying to fetch', country)
})
.then(response => {
.....
}
And I'm using a proxy to send that to an express server running on port 3001. The App.js file in the express server is the following: 我正在使用代理将其发送到运行在端口3001上的快递服务器。快递服务器中的App.js文件如下:
var express = require('express');
var app = express();
var cors = require('cors')
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
//app.use(logger('dev'));
// app.use(function(req, res, next) {
// res.header("Access-Control-Allow-Origin", "*");
// res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
// res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
// next();
// });
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', 'URLs to trust of allow');
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
if ('OPTIONS' == req.method) {
res.sendStatus(200);
} else {
next();
}
});
//app.use(cors())
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/users', users);
app.use('/schools', schools);
console.log("IN APP.JS");
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
And for some reason I get the following errors: 由于某种原因,我得到以下错误:
[Error] Failed to load resource: Preflight response is not successful (MR, line 0) [错误]无法加载资源:飞行前响应不成功(MR,第0行)
[Error] XMLHttpRequest cannot load http://localhost:3000/schools/countries/MR . [错误] XMLHttpRequest无法加载http:// localhost:3000 / schools / countries / MR 。 Preflight response is not successful 飞行前响应不成功
In the react side (front-end) I have this is the package.json file: 在反应端(前端),我有package.json文件:
"proxy": "http://localhost:3001",
I've tried using different headers in the App.js file and I have tried using the cors library to make this work etc. How can I fix this error ? 我尝试在App.js文件中使用不同的标头,并且尝试使用cors库进行此工作等。如何解决此错误?
Thanks! 谢谢!
I found the solution. 我找到了解决方案。 The problem what 1. I did have to remove the headers from the front end. 问题1.我确实必须从前端删除标头。 The second and probably most important thing is that when you're making a axios call using a proxy You should do not this: 第二个也是最重要的一点是,当您使用代理进行axios调用时,您不应这样做:
var api_url= "http:localhost" axios.get(api_url + '/schools/countries/' + country)
You should do this: 你应该做这个:
axios.get('/schools/countries/' + country)
without the localhost part. 没有本地主机部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.