問題:
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