【发布时间】:2017-10-02 16:00:20
【问题描述】:
我正在尝试通过服务功能(testing locally)使用xmlhttprequest调用firebase云功能,以下代码是xmlHttpRequest
var req = new XMLHttpRequest();
req.onload = function() {
//success
};
req.onerror = function() {
//always fall to this function
};
var url = 'https://us-central1-' + getFirebaseProjectId() + '.cloudfunctions.net/helloWorld';
req.open('GET', url);
//Chrome does not allow 'Access-Control-Allow-Origin' from
//localhost:5000 (non-google server)
//I am using mozilla firefox with 'serve' feature, hope
//this solve 'Access-Control-Allow-Origin' problem
req.setRequestHeader('Access-Control-Allow-Origin', '*');
req.send();
我可以看到云功能在 firebase 控制台中成功执行。但我总是陷入 onerror 函数。
我还通过
在云功能中启用了 CORSconst cors = require('cors')({origin: true});
在本地托管功能时出现以下错误
- 已将节点和 Firebase 更新为兼容版本。
- 已清除缓存
- 通过 npm 更新了 firebase-tools、firebase 功能
。还是不行。
【问题讨论】:
标签: firebase google-cloud-functions