Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从被CORS阻止的Firebase宿主代码调用Firebase函数_Javascript_Node.js_Firebase_Google Cloud Functions_Cors - Fatal编程技术网

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);});
我已尝试通过多种配置实现这一点:

  • 用于托管、调用已部署Firebase函数的Firebase emulator
  • 用于托管、调用仿真函数的Firebase emulator(Firebase函数模拟器)
  • 部署的主机调用了部署的Firebase函数
  • 所有配置都会产生以下结果:

    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((数据、上下文)
    资料来源:vs

    一般CORS检查表

    • 确保已部署该功能
    • 确保函数类型正确
      onCall
      vs
      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((数据、上下文)
    资料来源:vs

    一般CORS检查表

    • 确保已部署该功能
    • 确保函数类型正确
      onCall
      vs
      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