问题:
I'm currently learning how to use new Cloud Functions for Firebase and the problem I'm having is that I can't access the function I wrote through an AJAX request.我目前正在学习如何为 Firebase 使用新的 Cloud Functions,我遇到的问题是我无法访问我通过 AJAX 请求编写的函数。 I get the "No 'Access-Control-Allow-Origin'" error.我收到“没有'访问控制-允许-来源'”错误。 Here's an example of the function I wrote:这是我编写的函数的示例:
exports.test = functions.https.onRequest((request, response) => {
response.status(500).send({test: 'Testing functions'});
})
The function sits in this url: https://us-central1-fba-shipper-140ae.cloudfunctions.net/test该函数位于此 url: https : //us-central1-fba-shipper-140ae.cloudfunctions.net/test
Firebase docs suggests to add CORS middleware inside the function, I've tried it but it's not working for me: https://firebase.google.com/docs/functions/http-events Firebase 文档建议在函数中添加 CORS 中间件,我试过了,但对我不起作用: https : //firebase.google.com/docs/functions/http-events
This is how I did it:我是这样做的:
var cors = require('cors');
exports.test = functions.https.onRequest((request, response) => {
cors(request, response, () => {
response.status(500).send({test: 'Testing functions'});
})
})
What am I doing wrong?我究竟做错了什么? I would appreciate any help with this.我将不胜感激。
UPDATE:更新:
Doug Stevenson 's answer helped.道格史蒂文森的回答有所帮助。 Adding ({origin: true}) fixed the issue, I also had to change response.status(500)
to response.status(200)
which I completely missed at first.添加 ({origin: true}) 解决了这个问题,我还必须将response.status(500)
更改为response.status(200)
,我一开始完全错过了。
解决方案:
参考一: https://en.stackoom.com/question/2tOYt参考二: https://stackoom.com/question/2tOYt