在 firefox 和 safari 中,自定義標頭不會在 Service Worker 內部發送獲取請求。但是鉻有效。(我在 中添加了我的自定義標題Access-Control-Allow-Headers )
Service Worker 內部的代碼:
fetch(Request, {
headers: {
'my-custom-header': 'a value'
}
});
我的 OPTIONS 回傳 204,請求模式是“cors”。也是同源請求。
uj5u.com熱心網友回復:
(對于同源請求,CORS 不起作用。)
我無法重現您描述的問題;這是一個帶有 service worker 的站點的實時部署,該站點在啟動時向https://httpbin.org/#/Request_inspection/get_headers端點發出請求,并將回應記錄回來,指示收到了哪些請求標頭:
fetch('https://httpbin.org/headers', {
headers: {'my-custom-header': 'a value'},
})
.then((response) => response.json())
.then((data) => console.log(data.headers));
在 Chrome、Firefox 和 Safari 中,我看到自定義標頭與瀏覽器默認發送的其他標頭一起回傳。
您可以通過重新混合https://glitch.com/edit/#!/valuable-nonchalant-pet?path=sw.js:3:81來玩這個并測驗不同的場景
uj5u.com熱心網友回復:
我想通了我的錯誤。我傳遞的是 Request 物件而不是 URL。所以我應該這樣做:
const request = new Request(event.request);
request.headers.append('my-custom-header', 'a value');
return fetch(request);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/314688.html
標籤:javascript 火狐 苹果浏览器 拿来 服务人员
上一篇:在Firefox中檢測畫中畫模式
