简体   繁体   English

在Firebase托管中启用CORS

[英]Enabling CORS in Firebase Hosting

I have this really annoying issue with CORS and my Google Cloud function... 我对CORS和我的Google Cloud功能有这个非常烦人的问题......

the error I get is: 我得到的错误是:

Access to XMLHttpRequest at [ My function URL ] from origin ' http://localhost:4200 ' has been blocked by CORS policy: Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response. 来自“ http:// localhost:4200 ”的来自[ 我的函数URL ]的XMLHttpRequest访问已被CORS策略阻止:请求头字段access-control-allow-origin在预检中不允许使用Access-Control-Allow-origin响应。

Ive already tried to handle it on the function side with the following code: 我已经尝试使用以下代码在函数端处理它:

if request.method == 'OPTIONS':
        headers = {
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Methods': 'GET',
            'Access-Control-Allow-Headers': 'Content-Type',
            'Access-Control-Max-Age': '3600'
        }

        return ('', 204, headers)

and then added in the main return with: 然后在主要回报中添加:

headers = {
            'Access-Control-Allow-Origin': '*'
        }
....

return(json.dumps(data), 200, headers)

this doesnt work.... 这不起作用....

I also tried with the firebase.json headers approach and also not working... 我也试过firebase.json标头的方法,也没有工作......

even tried with the HttpHeaders in angular like so: 甚至尝试使用HttpHeaders角度如此:

const headers = new HttpHeaders() 
      .set("Access-Control-Allow-Origin", "*") 
      .set("Access-Control-Allow-Methods", "POST, GET, OPTIONS") 
      .set('cache-control', 'no-cache')

also not working..... 也没工作.....

then I tried with a proxy (just to check if that is the actual problem) added the proxy.config.json and of course in the dev it just works... 然后我尝试使用代理(只是为了检查这是否是实际问题)添加了proxy.config.json,当然在dev中它只是工作...

but the problem is, I need to be able to upload the Angular app to the firebase hosting so how can I fix the CORS error in the backend? 但问题是,我需要能够将Angular应用程序上传到firebase托管,以便如何修复后端的CORS错误?

One of the easiest way to get rid of cors errors is to use callable functions. 摆脱cors错误的最简单方法之一是使用可调用函数。 They also have the user context. 他们也有用户上下文。 Which can be really usefull. 这真的很有用。

But if you want to create an api. 但是如果你想创建一个api。 I would recommend using express and cors package. 我建议使用快递和cors包。 Here is the code that I use: 这是我使用的代码:

import * as express from 'express';
import * as cors from 'cors';

const app = express();
// cors middleware handles all the cors related issues.
app.use(cors({ origin: '*' }));

// you can add your routes in your app.
app.use('/users', usersRoute);

export const api = functions.https.onRequest(app);

Well its solved now, the issue: 那么它现在解决了,问题:

I ahve the CORS headers in the cloud functions (server side) AND in the webApp side too this somehow is not allowed so i just removed the code from the webapp side and left the code only in the cloud function (server side) this solved the problem... 我在云端功能(服务器端)和webApp端的CORS头也是不允许的,所以我只是从webapp端删除了代码,只留下了云功能(服务器端)的代码,这解决了问题...

Thanks to all! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM