Node.js 请求头在云函数中始终未定义
我正试图通过web客户端向我在Firebase中创建的云函数发送https请求。 云功能代码:Node.js 请求头在云函数中始终未定义,node.js,firebase,google-cloud-functions,Node.js,Firebase,Google Cloud Functions,我正试图通过web客户端向我在Firebase中创建的云函数发送https请求。 云功能代码: const functions = require('firebase-functions'); exports.app = (req, res) => { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Headers', 'Authorization, X-Reques
const functions = require('firebase-functions');
exports.app = (req, res) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Authorization, X-Requested-With');
res.header('Access-Control-Allow-Methods', 'GET, OPTIONS, POST');
console.log('authorization', req.get('authorization'));
res.status(200).send('authorization test');
};
Web客户端代码:
const data = JSON.stringify('/*data to send here...*/');
const url = '/* url endpoint here */'
const xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener('readystatechange', () => {
if (this.readyState === 4) {
/* success */
console.log(this.responseText);
}
});
xhr.open('POST', url);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Authorization', '/* auth token string here */');
xhr.setRequestHeader('Cache-Control', 'no-cache');
xhr.send(data);
在firebase函数的firebase控制台日志中,我始终将授权设置为未定义,即使我正在发送授权令牌,如中所述。您解决了相同的问题吗?@iLiner,我没有。我抛弃了
XHR
,使用fetch
和Content-Type
来application/json
。那帮我修好了。