我有一个通用的云功能:
const functions = require('firebase-functions');
const cors = require('cors')({ origin: true });
exports.helloWorld = functions.https.onRequest((request, response) => {
cors(request, response, () => {
res.status(200).send("Hello from Firebase!");
});
});
我正在使用 axios 从客户端调用它: axios
.get(
"https://us-central1-dev-imcla.cloudfunctions.net/helloWorld",
)
.then((res) => {
console.log(res);
})
.catch(er=>{
console.log(er);
})
我有两个问题:Access to XMLHttpRequest at 'https://myurl/helloWorld' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. xhr.js?b50d:178 GET https://us-central1-dev-imcla.cloudfunctions.net/helloWorld net::ERR_FAILED
Error: Forbidden Your client does not have permission to get URL /helloWorld from this server.
Error: Request failed with status code 403 at createError (createError.js?2d83:16) at settle (settle.js?467f:17) at XMLHttpRequest.handleLoad (xhr.js?b50d:61)
问题是我既是经过身份验证的用户,又在云代码中有 cors 包。
最佳答案
可能与 CORS 无关。检查 firebase 函数日志以查看代码中是否有任何错误。
https://stackoverflow.com/a/51103084/5781575
关于javascript - 带有axios请求的firebase云功能CORS错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62818364/