![](/img/trans.png)
[英]I am using heroku to test my application. how to solve cors problem?
[英]How to solve CORS problem with my Angular App on Heroku
对不起,如果我的问题已经有答案了..但我已经做了所有的事情并且无法解决它......
我在 heroku 上托管了 2 个项目,一个用 angular 制作的前端和一个用 java (spring boot) 制作的登录后端。 问题是当我尝试登录时遇到了 CORS 问题
No 'Access-Control-Allow-Origin' header is present on the requested resource.
当我使用 Postman 请求我的 springboot 服务器时,它通常可以工作...
另外,这是我用于 heroku 部署的 server.js
const express = require('express');
const path = require('path');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(express.static('./dist/fonetApp'));
app.get('/',function(req,res){
res.sendFile(path.join(__dirname+'/dist/fonetApp/index.html'));
});
app.listen(process.env.PORT || 8080);
请..帮助t_t
我通过使用cors
在 heroku 上解决了同样的问题
const express = require('express');
const path = require('path');
const cors = require('cors');
const app = express();
// add this code
const whitelist = ['http://localhost:3000']; // list of allow domain
const corsOptions = {
origin: function (origin, callback) {
if (!origin) {
return callback(null, true);
}
if (whitelist.indexOf(origin) === -1) {
var msg = 'The CORS policy for this site does not ' +
'allow access from the specified Origin.';
return callback(new Error(msg), false);
}
return callback(null, true);
}
}
// end
app.use(cors(corsOptions));
app.use(express.static('./dist/fonetApp'));
app.get('/',function(req,res){
res.sendFile(path.join(__dirname+'/dist/fonetApp/index.html'));
});
app.listen(process.env.PORT || 8080);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.