我的FE的應用程序使用API從不同域發送。我知道它應該觸發CORS,但據我瞭解,它不應該爲每個請求創建預檢。預檢請求與所有方法
據docs,我不應該有GET
方法預檢要求。
Cross-site requests are preflighted like this since they may have implications to
user data. In particular, a request is preflighted if:
- It uses methods other than GET, HEAD or POST.
Also, if POST is used to send request data with a Content-Type
other than application/x-www-form-urlencoded, multipart/form-data,
or text/plain, e.g. if the POST request sends an XML payload to the
server using application/xml or text/xml, then the request is preflighted.
- It sets custom headers in the request
(e.g. the request uses a header such as X-PINGOTHER)
但是,我要送每一個要求,具有預檢(期權)的要求,不管它是GET或POST,我覺得很奇怪(按照什麼文檔說的)。
我設置一些標題(和我與withCredentials: true
發送),但我不認爲它應該是這個問題:
headers.append('Access-Control-Allow-Origin', FRONTEND_URL);
headers.append('Accept', 'application/json');
headers.append('Content-Type', 'application/json');
headers.append('Authorization', this._generateApiKey());
headers.append('Language', this._languageISOCode);
我這麼想嗎?
WithCredentials是你的自定義頭,這意味着它被用於預檢GET/POST請求 – Icepickle