admin管理员组文章数量:1392007
I cannot get my Firebase http function working when calling it from the client. Here are the errors on the client:
OPTIONS 500 ()
Failed to load :
Response to preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:3000' is therefore not allowed access.
The response had HTTP status code 500.
Here is what my function looks like:
const admin = require('firebase-admin');
const functions = require('firebase-functions');
const cors = require('cors')({origin: true});
exports.func = functions.https.onRequest((req, res) => {
return cors((req, res, () => {
res.send("hello");
}));
});
The Firebase functions log is providing this information:
TypeError: Cannot read property 'origin' of undefined
at /user_code/node_modules/cors/lib/index.js:219:39
at optionsCallback (/user_code/node_modules/cors/lib/index.js:199:9)
at corsMiddleware (/user_code/node_modules/cors/lib/index.js:204:7)
at exports.pay.functions.https.onRequest (/user_code/my-func.js:11:10)
Note that 11:10 in the call stack is this: return cors((req, res, () => {
The request I'm making is sending an object with a single key:value pair in the request body. I added the cors
middleware based on other questions I've seen on SO, but no luck!
Edit: Adding the request code:
function post(url, obj) {
return new Promise((resolve, reject) => {
const req = new XMLHttpRequest();
req.open('POST', url, true);
req.setRequestHeader("Content-type", "application/json");
req.onload = function() {
if(req.status == 200) {
return resolve(req.response);
}
else {
return reject(Error(req.statusText));
}
};
req.onerror = function() {
return reject(Error("Network Error"));
};
req.send(JSON.stringify(obj));
});
}
I cannot get my Firebase http function working when calling it from the client. Here are the errors on the client:
OPTIONS https://us-central1-my-app.cloudfunctions/my-func 500 ()
Failed to load https://us-central1-my-app.cloudfunctions/my-func:
Response to preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:3000' is therefore not allowed access.
The response had HTTP status code 500.
Here is what my function looks like:
const admin = require('firebase-admin');
const functions = require('firebase-functions');
const cors = require('cors')({origin: true});
exports.func = functions.https.onRequest((req, res) => {
return cors((req, res, () => {
res.send("hello");
}));
});
The Firebase functions log is providing this information:
TypeError: Cannot read property 'origin' of undefined
at /user_code/node_modules/cors/lib/index.js:219:39
at optionsCallback (/user_code/node_modules/cors/lib/index.js:199:9)
at corsMiddleware (/user_code/node_modules/cors/lib/index.js:204:7)
at exports.pay.functions.https.onRequest (/user_code/my-func.js:11:10)
Note that 11:10 in the call stack is this: return cors((req, res, () => {
The request I'm making is sending an object with a single key:value pair in the request body. I added the cors
middleware based on other questions I've seen on SO, but no luck!
Edit: Adding the request code:
function post(url, obj) {
return new Promise((resolve, reject) => {
const req = new XMLHttpRequest();
req.open('POST', url, true);
req.setRequestHeader("Content-type", "application/json");
req.onload = function() {
if(req.status == 200) {
return resolve(req.response);
}
else {
return reject(Error(req.statusText));
}
};
req.onerror = function() {
return reject(Error("Network Error"));
};
req.send(JSON.stringify(obj));
});
}
Share
Improve this question
edited Feb 1, 2018 at 23:11
Frank van Puffelen
601k85 gold badges890 silver badges860 bronze badges
asked Feb 1, 2018 at 22:30
skwnyskwny
3,1806 gold badges31 silver badges52 bronze badges
3
- What does the full request look like? Code for that? – Doug Stevenson Commented Feb 1, 2018 at 22:43
-
@DougStevenson, added the code. I call the
post
method and pass in my URL and obj. – skwny Commented Feb 1, 2018 at 22:47 - Multiple incidents may affect at present Cloud Functions for Firebase. Your issue might derive from this situation. You can follow developments on the Firebase Status Dashboard page. – George Commented Feb 2, 2018 at 1:08
1 Answer
Reset to default 9Programmer error, as usual:
cors((req, res, () => {
res.send("hello");
}));
should be:
cors(req, res, () => {
res.send("hello");
});
Had an extra set of parens around the cors
args.
本文标签: javascriptFirebase HTTP function CORS issueStack Overflow
版权声明:本文标题:javascript - Firebase HTTP function CORS issue - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744760962a2623753.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论