• <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'>

        获取响应中未显示标头

        Headers not showing in fetch response(获取响应中未显示标头)

          <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]}`);
                    }
                  });

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  业务场景:使用update语句去更新数据库字段。 原因:update接收值不正确。原来代码: $query = "UPDATE student SET date = now() WHERE id = $id";$result = $mysqli-query($query2) or die($mysqli-error); // 问题出现了在这句 $data = $result-fetch_ass
                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                • <tfoot id='Ga78i'></tfoot>
                    <tbody id='Ga78i'></tbody>

                    <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>
                      • <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>