简体   繁体   English

如何使用 Heroku 上的 Angular App 解决 CORS 问题

[英]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.

相关问题 我正在使用 heroku 来测试我的应用程序。 如何解决 cors 问题? - I am using heroku to test my application. how to solve cors problem? 对于部署在 heroku 上的 Angular 应用程序,我该如何处理 CORS 问题? - How do I deal with CORS problem for Angular app deployed on heroku with a Postgres express backend? 如何解决我的 heroku 应用程序上的“不安全”警告? - How to solve "Not Secure" warning on my heroku app? MERN 应用程序 on.netlify 和 heroku (CORS) 的问题 - Problem with MERN app on netlify and heroku (CORS) 在 heroku 中部署 Angular 应用程序时出现问题 - Problem in deploying Angular app in heroku 使用 angular 9 部署在 heroku 上的 nodejs 应用程序中出现 CORS 错误 - CORS error in nodejs app deployed on heroku using with angular 9 如何解决我的 MERN 应用程序中的 Access-Control-Allow-Origin CORS 错误以进行 MSAL 身份验证? - How can I solve the Access-Control-Allow-Origin CORS error in my MERN app for MSAL auth? 我在 Heroku 上部署了我的应用程序,但后来我遇到了 cors 问题 - I deployed my app on Heroku but then I have cors problems 如何解决heroku中的“应用崩溃”错误 - How to solve “App crash” error in heroku 如何解决在 Heroku 上部署 React 应用程序时出现的错误 - How to solve the Error in deploying react app on Heroku
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM