我使用 C# 構建了一個 API,該 API 使用 JWT 令牌進行授權。在前端,我將這些令牌存儲在本地存儲中,并在創建請求時獲取它們。創建 GET 或 DELETE 請求時,一切正常,并且使用console.log()我可以看到提取選項添加了授權標頭。但是,當使用 POST 或 PATCH 方法時,授權標頭在將其添加到對象后立即丟失。這是我的請求方法:const send = async (apiOptions: ApiParams): Promise<FetchReturn> => { const accessToken = GetAccessToken() const options: ApiOptions = { method: apiOptions.method, headers: { Authorization: `Bearer ${accessToken}` } } console.log(options) if (apiOptions.data) { options.headers = { 'Content-Type': 'application/json' } options.body = JSON.stringify(apiOptions.data) } const result = await fetch(`${getUrl()}/${apiOptions.path}`, options).then(res => res).catch(err => err) if (!result.ok) { if (IsExpired()) { const refreshResult = await fetch(`${getUrl()}/api/user/refresh`, {method: 'POST', headers:{ 'Content-Type': 'application/json' }, body: JSON.stringify(GetRefreshRequest())}).then(res => res).catch(err => err) if (refreshResult.ok) { Login(JSON.parse(await refreshResult.text())) return await send(apiOptions) } else if (refreshResult.status === 401) { Logout() window.location.reload() return { code: 0, text: ""} } } } const text = await result.text() return { code: result.status, text: text }}
Typescript 從 POST 和 PATCH 獲取請求中刪除 Authorization 標頭
慕妹3242003
2023-08-05 11:52:29
