[英]How to solve CORS problem with my Angular App on Heroku
Sorry if there is already an answer for my problem.. but i already did all and can't solve it...对不起,如果我的问题已经有答案了..但我已经做了所有的事情并且无法解决它......
I have 2 projects hosted on heroku, a frontend made with angular and a login backend with java (spring boot).我在 heroku 上托管了 2 个项目,一个用 angular 制作的前端和一个用 java (spring boot) 制作的登录后端。 The problem is when i try to login i got the CORS problem
问题是当我尝试登录时遇到了 CORS 问题
No 'Access-Control-Allow-Origin' header is present on the requested resource.
When i request my springboot server with Postman, it normally works...当我使用 Postman 请求我的 springboot 服务器时,它通常可以工作...
Also, this is my server.js for the heroku deploy另外,这是我用于 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);
Pleasee.. help t_t请..帮助t_t
I solved same issue on heroku by using cors
我通过使用
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.