【问题标题】:Fetch request with token in Header使用标头中的令牌获取请求
【发布时间】:2018-07-05 14:26:39
【问题描述】:

当我在地址“http://localhost:8080/clients”上执行获取请求时,我需要帮助以在标头中包含令牌。

现在我收到这条消息“HTTP 403 Forbidden”。

授权令牌 1234abcd

function getAllClients() {
      const myHeaders = new Headers();
      myHeaders.append('Content-Type', 'application/json');

      return fetch('http://localhost:8080/clients', {
        method: 'GET',
        mode: 'no-cors',
        headers: myHeaders,
      })
        .then(response => response.json())
        .then((user) => {
          console.log(user.name);
          console.log(user.location);
        })
        .catch((error) => {
          console.error(error);
        });
    }

    getAllClients();

【问题讨论】:

  • 您是否尝试在标头中附加令牌?提供有关您创建的 API 以及如何调用令牌的更多信息。

标签: javascript fetch fetch-api


【解决方案1】:

使用fetch(),当启用no-cors 模式时,您无法发送Authorization 标头。

no-cors — 防止方法成为 HEAD、GET 以外的任何东西 或 POST,并且标题不是简单的 标题。

https://developer.mozilla.org/en-US/docs/Web/API/Request/mode

什么是简单标题?

  • Accept
  • Accept-Language
  • Content-Language
  • Content-Type,其值一旦提取,其 MIME 类型(忽略参数)为application/x-www-form-urlencodedmultipart/form-data,或text/plain

https://fetch.spec.whatwg.org/#simple-header

所以你的问题出在下面一行:

mode: 'no-cors',

只需将其从 fetch 请求中删除并照常附加您的 Authorization 标头即可。

const myHeaders = new Headers();

myHeaders.append('Content-Type', 'application/json');
myHeaders.append('Authorization', '1234abcd');

return fetch('http://localhost:8080/clients/', {
  method: 'GET',
  headers: myHeaders,
})

希望对你有帮助:)

【讨论】:

    【解决方案2】:

    有多种方法可以在请求中设置header,可以查看文档here

    这里是更新的代码:

    function getAllClients() {
    const myHeaders = new Headers({
        'Content-Type': 'application/json',
        'Authorization': 'your-token'
    });
    
    return fetch('http://localhost:8080/clients', {
      method: 'GET',
      headers: myHeaders,
    })
    
    .then(response => {
        if (response.status === 200) {
          return response.json();
        } else {
          throw new Error('Something went wrong on api server!');
        }
      })
      .then(response => {
        console.debug(response);
      }).catch(error => {
        console.error(error);
      });
    }
    
    getAllClients();
    

    【讨论】:

      【解决方案3】:

      这就是你需要的:

       function getAllClients() {
        const myHeaders = new Headers();
      
        /* 
          myHeaders.append('Content-Type', 'application/json'); 
          since it's a get request you don't need to specify your content-type
        */
      
        myHeaders.append('Authorization', 'Token 1234abcd');
      
        return fetch('http://localhost:8080/clients', {
          method: 'GET',
          mode: 'no-cors',
          headers: myHeaders,
        })
          .then(response => response.json())
          .then((user) => {
            console.log(user.name);
            console.log(user.location);
          })
          .catch((error) => {
            console.error(error);
          });
      }
      
      getAllClients();
      

      【讨论】:

      • 这很有帮助,但只是没有“模式:'no-cors'”。谢谢法比恩
      猜你喜欢
      • 2020-11-16
      • 2017-07-28
      • 2017-09-22
      • 1970-01-01
      • 2021-03-11
      • 2017-10-29
      • 2018-12-23
      • 2021-01-21
      • 2019-11-26
      相关资源
      最近更新 更多