Home >Web Front-end >JS Tutorial >How to Configure CORS for Firebase Cloud Functions?
CORS Configuration in Cloud Functions for Firebase
When accessing Cloud Functions for Firebase through AJAX requests, you may encounter the "No 'Access-Control-Allow-Origin'" error indicating improper CORS configuration.
For Cloud Functions v2:
Firebase-functions/v2/https allows you to enable CORS directly within the function definition:
const { onRequest } = require("firebase-functions/v2/https"); exports.sayHello = onRequest({ cors: true }, (req, res) => { res.status(200).send("Hello world!"); });
For Cloud Functions v1:
Two Firebase-provided samples demonstrate the use of CORS:
The second sample employs a different approach than you've been using:
const cors = require("cors")({ origin: true }); exports.fn = functions.https.onRequest((req, res) => { cors(req, res, () => { // Your function body here }); });
Steps to Resolve CORS Issues:
The above is the detailed content of How to Configure CORS for Firebase Cloud Functions?. For more information, please follow other related articles on the PHP Chinese website!