• <tfoot id='dAy4O'></tfoot>
    <legend id='dAy4O'><style id='dAy4O'><dir id='dAy4O'><q id='dAy4O'></q></dir></style></legend>

        <i id='dAy4O'><tr id='dAy4O'><dt id='dAy4O'><q id='dAy4O'><span id='dAy4O'><b id='dAy4O'><form id='dAy4O'><ins id='dAy4O'></ins><ul id='dAy4O'></ul><sub id='dAy4O'></sub></form><legend id='dAy4O'></legend><bdo id='dAy4O'><pre id='dAy4O'><center id='dAy4O'></center></pre></bdo></b><th id='dAy4O'></th></span></q></dt></tr></i><div id='dAy4O'><tfoot id='dAy4O'></tfoot><dl id='dAy4O'><fieldset id='dAy4O'></fieldset></dl></div>

          <bdo id='dAy4O'></bdo><ul id='dAy4O'></ul>
      1. <small id='dAy4O'></small><noframes id='dAy4O'>

        获取响应中未显示标头

        时间:2023-10-02

          <tbody id='TiR68'></tbody>
          <bdo id='TiR68'></bdo><ul id='TiR68'></ul>
          <i id='TiR68'><tr id='TiR68'><dt id='TiR68'><q id='TiR68'><span id='TiR68'><b id='TiR68'><form id='TiR68'><ins id='TiR68'></ins><ul id='TiR68'></ul><sub id='TiR68'></sub></form><legend id='TiR68'></legend><bdo id='TiR68'><pre id='TiR68'><center id='TiR68'></center></pre></bdo></b><th id='TiR68'></th></span></q></dt></tr></i><div id='TiR68'><tfoot id='TiR68'></tfoot><dl id='TiR68'><fieldset id='TiR68'></fieldset></dl></div>

                • <legend id='TiR68'><style id='TiR68'><dir id='TiR68'><q id='TiR68'></q></dir></style></legend>
                • <tfoot id='TiR68'></tfoot>

                • <small id='TiR68'></small><noframes id='TiR68'>

                  本文介绍了获取响应中未显示标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I can see the header "x-auth-token" in Chrome DevTools.

                  However, the header is not showing up in my fetch response. Please assist so I can use header data.

                  I am using NodeJS as my backend API and ReactJS as my front-end. These are my files.

                  NodeJS middleware - cors.js

                  module.exports = function enableCorsSupport(app) {
                    app.use(function(req, res, next) {
                      res.header("Access-Control-Allow-Origin", "*");
                      res.header("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE");
                      res.header("Access-Control-Allow-Headers", "Content-Type, x-auth-token");
                      res.header("Access-Control-Expose-Headers", "x-auth-token");
                      next();
                    })
                  }
                  

                  NodeJS route - users.js

                  router.post('/login', async (req, res) => {
                  
                    // NOTE: code left out so post would be smaller
                  
                    const token = user.generateAuthToken();
                    res.header('x-auth-token', token).send(_.pick(user, ['_id', 'firstName', 'email', 'isAdmin']));
                  })
                  

                  ReactJS - my fetch request

                  fetch('http://localhost:4000/api/users/login', {
                      method: 'POST',
                      headers: {
                        'Content-Type': 'application/json',
                      },
                      body: JSON.stringify({
                        email: this.props.email,
                        password: this.props.password
                      })
                    })
                    .then(res => {
                      console.log('res.headers', res.headers)
                      return res.json()
                    })
                    .then(data => {
                      console.log(data);
                    })
                    .catch((err) => {
                      console.log(err)
                    })
                  }
                  

                  This is in my Chrome console from the console.log in my successful fetch request. Headers are empty in the header response. Please advise. FYI this is user test data.

                  解决方案

                  The Header object is not empty. It is just not a regular object so it doesn't have its contents as properties on its instance. As such you won't see the headers / values in a console.log view.

                  To get a particular header's value you need to use the get() method

                  var token = response.headers.get('x-auth-token');
                  console.log(token);
                  

                  You can also loop through it using for ... of

                  for(const header of response.headers){
                     console.log(header);
                  }
                  

                  Demo

                  fetch('https://cors-anywhere.herokuapp.com')
                  .then(res=>{
                    for(const header of res.headers){
                      console.log(`Name: ${header[0]}, Value:${header[1]}`);
                    }
                  });

                  这篇关于获取响应中未显示标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用从 GitHub API 获取数据动态填充我的表? 下一篇:反应原生 fetch 不调用 then 或 catch

                  相关文章

                • <tfoot id='Ga78i'></tfoot>

                    <small id='Ga78i'></small><noframes id='Ga78i'>

                      <bdo id='Ga78i'></bdo><ul id='Ga78i'></ul>
                    <legend id='Ga78i'><style id='Ga78i'><dir id='Ga78i'><q id='Ga78i'></q></dir></style></legend>
                  1. <i id='Ga78i'><tr id='Ga78i'><dt id='Ga78i'><q id='Ga78i'><span id='Ga78i'><b id='Ga78i'><form id='Ga78i'><ins id='Ga78i'></ins><ul id='Ga78i'></ul><sub id='Ga78i'></sub></form><legend id='Ga78i'></legend><bdo id='Ga78i'><pre id='Ga78i'><center id='Ga78i'></center></pre></bdo></b><th id='Ga78i'></th></span></q></dt></tr></i><div id='Ga78i'><tfoot id='Ga78i'></tfoot><dl id='Ga78i'><fieldset id='Ga78i'></fieldset></dl></div>