我是这个谷歌云 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 配置文件
我做错了(打电话)吗?提前致谢