• 请不要在回答技术问题时复制粘贴 AI 生成的内容
Lukedis
V2EX  ›  程序员

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

  •  
  •   Lukedis ·
    Lukedis · Jan 23, 2023 · 2402 views
    This topic created in 1380 days ago, the information mentioned may be changed or developed.
    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 replies  •  2023-01-24 11:50:32 +08:00
    lisongeee
    lisongeee
    Jan 23, 2023
    1
    ```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
    icebay
    Jan 23, 2023
    2
    'Authorization': `${localStorage.getItem('Authorization')}`,

    'Authorization': localStorage.getItem('Authorization') || '',
    Lukedis
    Lukedis
    OP
    Jan 24, 2023
    3
    @lisongeee 在不使用!的情况下,还有哪些解决办法
    bgm004
    bgm004
    Jan 24, 2023 via Android❤️1
    4
    不想用!。那就只能提前类型收窄了。
    musi
    musi
    Jan 24, 2023
    5
    as string
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5175 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 01:49 · PVG 09:49 · LAX 17:49 · JFK 20:49
    ♥ Do have faith in what you're doing.