简体   繁体   English

为请求设置标题时,NodeJS http服务器响应标题为空

[英]NodeJS http server response headers are blank when headers are set for a request

So I'm sending a request via ajax and I'm setting the headers there and I can log and see that the headers are indeed being set 所以我通过ajax发送请求,并在其中设置标头,我可以登录并看到标头确实已设置

$.ajax({
    headers: {
        Authorization: localStorage.getItem('t')
    },
    type: 'GET',
    dataType: 'json',
    url: `/updatebackground?b=${bg}`,
    success: function(data) {
        console.log('yay');
    },
    complete: function() {
        console.log(this.headers)
    }
});

But when I try to log the headers in node they come back as blank 但是当我尝试将标头记录在节点中时,它们以空白形式返回

http.createServer(async (req, res) => {
    console.log(JSON.stringify(res._headers));
});

logs "{}" I'm unsure as to why this happens and any help would be appreciated 日志"{}"我不确定为什么会发生这种情况,我们将不胜感激

Apparenly it works with req.headers.authorization Doesn't really make sense as to why it's lowercase but eh whatever. 显然,它可以与req.headers.authorization一起使用。为何它是小写的并没有任何意义。 I'll keep this here for anyone else with the same issue. 我将把这个问题留在这里给其他有同样问题的人使用。

On the server, you're trying to access the headers on the response object, not the request object. 在服务器上,您尝试访问响应对象(而不是请求对象)上的标头。 Since the client set the headers on the request the desired headers would be on the request, or req . 由于客户端在请求上设置标头,因此所需的标头将在请求或req

If you change that server-side code to: 如果您将该服务器端代码更改为:

http.createServer(async (req, res) => {
    console.log(req.headers);
});

You will see the request headers 您将看到请求标头

This is because you are logging the res headers and not the req headers. 这是因为您正在记录res标头,而不是req标头。

the proper code for logging the request headers is: 记录请求标头的正确代码是:

http.createServer(async (req, res) => {
    console.log(JSON.stringify(req.headers));
});

res._headers is for setting the headers for the response that is sent by the server to client as the request's answer. res._headers用于设置服务器发送到客户端作为请求答案的响应的标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 NodeJS 应用程序中发出 HTTP 请求时,何时显式设置标头 - When to set the headers explicitly when making HTTP request in NodeJS application Node.js HTTP请求,无法更改响应头 - Nodejs HTTP request, cant alter response headers AngularJS无法从NodeJS服务器读取响应HTTP标头 - AngularJS can't read response HTTP Headers from NodeJS server 使用 NodeJS 设置请求标头的 HTTPS - HTTPS with NodeJS set request headers 在nodejs中返回响应时,无法在将标头发送到客户端后设置标头 - Cannot set headers after they are sent to the client when return the response in nodejs 可以使用 nodejs 和 puppeteer 获取 HTTP 响应标头吗? - Possible to get HTTP response headers with nodejs and puppeteer? 更新nodejs http.request中的标头 - Update headers in a nodejs http.request ERR_HTTP_HEADERS_SENT:在服务器响应中将标头发送到客户端后无法设置标头 - ERR_HTTP_HEADERS_SENT: Cannot set headers after they are sent to the client at Server Response 错误 [ERR_HTTP_HEADERS_SENT]:在向客户端发出请求时无法设置标头,当向 API 发出请求时 - Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client, when making request to API 在内部发出另一个HTTP请求后,发送HTTP响应时出错:“发送后无法设置标题” - Error on sending http response after making another http request inside: “Can't set headers after they are sent”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM