V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
Lukedis
V2EX  ›  程序员

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

  •  
  •   Lukedis ·
    Lukedis · Jan 23, 2023 · 1871 views
    This topic created in 1195 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
        1
    lisongeee  
       Jan 23, 2023
    ```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  
       Jan 23, 2023
    'Authorization': `${localStorage.getItem('Authorization')}`,

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