Javascript 从被CORS阻止的Firebase宿主代码调用Firebase函数
我已经用Javascript 从被CORS阻止的Firebase宿主代码调用Firebase函数,javascript,node.js,firebase,google-cloud-functions,cors,Javascript,Node.js,Firebase,Google Cloud Functions,Cors,我已经用函数和托管功能初始化了一个模板化的Firebase项目 我已取消对模板化HTTP函数的注释: export const helloWorld=functions.https.onRequest((req,res)=>{ functions.logger.info(“Hello logs!”,{structuredData:true}); res.send(“来自Firebase的你好!”); }); 并将以下代码添加到模板化的public/index.html文件中: const fu
函数
和托管
功能初始化了一个模板化的Firebase项目
我已取消对模板化HTTP函数的注释:
export const helloWorld=functions.https.onRequest((req,res)=>{
functions.logger.info(“Hello logs!”,{structuredData:true});
res.send(“来自Firebase的你好!”);
});
并将以下代码添加到模板化的public/index.html
文件中:
const functions=firebase.functions();
const helloWorld=functions.httpscalable('helloWorld');
helloWorld().then((res)=>{console.log(res);});
我已尝试通过多种配置实现这一点:
Access to fetch at 'https://us-central1-my-project.cloudfunctions.net/helloWorld' from origin 'http://127.0.0.1:5000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
在模板化的、自动生成的Firebase代码中,我没有改变任何东西,没有我说过的东西
我遗漏了什么?您将
onRequest
与onCall
错误匹配。这两种方法不兼容,导致CORS错误,因为它找不到
更改您的http类型:
- From:
.onRequest((请求,请求)
- 收件人:
.onCall((数据、上下文)
- 确保已部署该功能
- 确保函数类型正确
vsonCall
onRequest
- 确保函数名正确。打字错误和大小写很重要
- 确保函数代码本身没有抛出错误。Emulator可帮助调试
- 确保您的区域将函数与该区域匹配,并使用该区域调用函数
const functions=require('firebase-functions');
exports.yourFunc=functions.region('europe-west2').https.onCall(异步(数据、上下文)=>{
// ...
})
从客户端调用特定区域中的函数:
从“firebase/app”导入firebase
导入“firebase/functions”
firebase.app().functions('europe-west2').HttpScalable('yourFunc'))
注意:firebase.app()函数…与firebase.app.函数。。。
您将onRequest
与onCall
不匹配这两种不同的方法不兼容,导致CORS错误,因为它找不到
更改您的http类型:
- From:
.onRequest((请求,请求)
- 收件人:
.onCall((数据、上下文)
- 确保已部署该功能
- 确保函数类型正确
vsonCall
onRequest
- 确保函数名正确。打字错误和大小写很重要
- 确保函数代码本身没有抛出错误。Emulator可帮助调试
- 确保您的区域将函数与该区域匹配,并使用该区域调用函数
const functions=require('firebase-functions');
exports.yourFunc=functions.region('europe-west2').https.onCall(异步(数据、上下文)=>{
// ...
})
从客户端调用特定区域中的函数:
从“firebase/app”导入firebase
导入“firebase/functions”
firebase.app().functions('europe-west2').HttpScalable('yourFunc'))
注意:firebase.app()函数…与firebase.app.函数。。。
你实际上是在混淆视听
您的helloWorld
云函数代码对应于HTTP代码,但前端(即public/index.html
)中的代码调用可调用的代码
您应该将
helloWorld
Cloud函数作为RESTAPI调用,例如,使用fetch或Axios。您实际上是在混淆和
您的helloWorld
云函数代码对应于HTTP代码,但前端(即public/index.html
)中的代码调用可调用的代码
您应该将helloWorld
Cloud函数作为restapi调用,例如使用fetch或Axios