-1

我是这个谷歌云 API 网关的新手。我按照文档中的每一步创建了一个 API 网关并取得了成功。我正在使用具有不同后端(如 Google Cloud Function)的 Google API Gateway。

https://cloud.google.com/api-gateway/docs/quickstart

我通过 curl 命令对此进行了测试,它工作正常 - 就像从后端(云函数)获取响应一样。现在,我想以编程方式进行。从网站调用 API 网关。当我尝试这样做时,我收到如下错误

错误:

Access to XMLHttpRequest at 'https://[mygatewayid].uc.gateway.dev/hello2/' from origin 'http://localhost:5000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
    
POST https://[mygatewayid].uc.gateway.dev/hello2/ net::ERR_FAILED

我从 Web 调用的 API

$.ajax({
     type: "post",
     url: "https://[mygatewayid].uc.gateway.dev/hello2/",
     data:{
         PhoneNumber: phonum
     },
     async: true,
     cache: false,
     success: function(data,status) {
         console.log("Data: " + data);
     },
     error : function(x, e) { 
         console.log(x.responseText); 
     }
 });

我的云功能

exports.hello2 = functions.https.onRequest((req,res)=>{

    res.header("Access-Control-Allow-Origin","*");
    res.header("Access-Control-Allow-Headers","Origin,X-Requested-With,Content-Type,Accept");

    var phonum = req.body.PhoneNumber;
    console.log("phonum",phonum);

    let message = req.query.PhoneNumber || req.body.PhoneNumber || 'Hello World!';

    res.status(200).send(message);
});

我的 API 配置文件

在此处输入图像描述

我做错了(打电话)吗?提前致谢

4

1 回答 1

1

该功能尚不可用,应该很快就会到来。现在,您可以在您的 openAPI 规范中添加一个 OPTION,就像这样

paths:
  "/hello2":
    options:
      operationId: create-cors
      responses:
        200:
          description: Success
    get:
      ........
于 2021-08-02T13:32:18.983 回答