Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 请求头在云函数中始终未定义_Node.js_Firebase_Google Cloud Functions - Fatal编程技术网

Node.js 请求头在云函数中始终未定义

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

我正试图通过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-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
。那帮我修好了。