V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Lukedis
V2EX  ›  程序员

fetch header 携带自定义请求头,类型不匹配

  •  
  •   Lukedis ·
    Lukedis · 2023-01-23 22:09:15 +08:00 · 1051 次点击
    这是一个创建于 430 天前的主题,其中的信息可能已经有所发展或是发生改变。
    fetch(`${url.baseUrl}${url.user.auth}`, {
        method: 'GET',
        headers: {
          'Content-Type': 'application/json',
          //'Authorization': localStorage.getItem('Authorization'),
        },
      }).then((response) => {
        if (response.status == 200) {
          auth = true;
        }
      });
    

    localStorage.getItem('Authorization')type 是 string|null 和headers要求的参数类型不匹配

    5 条回复    2023-01-24 11:50:32 +08:00
    lisongeee
        1
    lisongeee  
       2023-01-23 22:20:38 +08:00
    ```ts
    fetch(`${url.baseUrl}${url.user.auth}`, {
    method: 'GET',
    headers: {
    'Content-Type': 'application/json',
    'Authorization': localStorage.getItem('Authorization')!,
    },
    }).then((response) => {
    if (response.status == 200) {
    auth = true;
    }
    });
    ```
    IceBay
        2
    IceBay  
       2023-01-23 22:38:40 +08:00
    'Authorization': `${localStorage.getItem('Authorization')}`,

    'Authorization': localStorage.getItem('Authorization') || '',
    Lukedis
        3
    Lukedis  
    OP
       2023-01-24 08:10:28 +08:00
    @lisongeee 在不使用!的情况下,还有哪些解决办法
    Track13
        4
    Track13  
       2023-01-24 09:05:30 +08:00 via Android   ❤️ 1
    不想用!。那就只能提前类型收窄了。
    musi
        5
    musi  
       2023-01-24 11:50:31 +08:00
    as string
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3233 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 14:23 · PVG 22:23 · LAX 07:23 · JFK 10:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.