๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Daily/Today I Learned

210206_TIL

by joooing 2021. 2. 7.
๋ฐ˜์‘ํ˜•
๐ŸŽ ์˜ค๋Š˜ ํ•œ ์ผ
โœ”๏ธ CORS ์ •๋ฆฌ
CORS์— ์š”์ฒญ๋ฐฉ๋ฒ•์ด ์„ธ ๊ฐ€์ง€๊ฐ€ ์žˆ๋Š”๋ฐ, Preflight Request, Simple Request, Credential Request ์ด๋ ‡๊ฒŒ ๋‚˜๋‰œ๋‹ค. ๊ฐ๊ฐ์— ์กฐ๊ฑด๋“ค์ด ์žˆ์–ด์„œ ๊ทธ ๋ถ€๋ถ„์— ๋Œ€ํ•ด ์ •๋ฆฌํ–ˆ๋‹ค. ํŠนํžˆ CORS๋ฅผ ํŒ๋‹จํ•˜๋Š” ์‹œ์ ์ด ์ค‘์š”ํ•˜๋‹ค๊ณ  ๋Š๊ปด์กŒ๋Š”๋ฐ, ํŒ๋‹จ + ์‘๋‹ต ํŒŒ๊ธฐ ์—ฌ๋ถ€๋Š” ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๊ฒฐ์ •
ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์š”์ฒญ์˜ ์‹คํŒจ๋‚˜ ์„ฑ๊ณต์— ์ƒ๊ด€์—†์ด ๊ทธ ์ดํ›„์— CORS ์œ„๋ฐ˜ ์—ฌ๋ถ€๊ฐ€ ํŒ๋‹จ๋œ๋‹ค. ์ด ์ ์€ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด๋ฉด์„œ ์ง์ ‘ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

โœ”๏ธ CORS ํ…Œ์ŠคํŠธ
ํด๋ผ์ด์–ธํŠธ๋ž‘ ์„œ๋ฒ„๋ฅผ ๋ถ„๋ฆฌํ•ด์„œ CORS์™€ ๊ด€๋ จ๋œ ํ…Œ์ŠคํŠธ๋“ค์„ ํ•ด๋ดค๋‹ค. ํฌํŠธ๋ฒˆํ˜ธ๊ฐ€ ๋™์ผํ•  ๋•Œ์™€ ๋‹ค๋ฅผ ๋•Œ, Allow-Control-Allow-Origin ๊ฐ’์„ * ๋กœ ํ–ˆ์„ ๋•Œ์™€ origin์œผ๋กœ ์„ค์ •ํ–ˆ์„ ๋•Œ, ์š”์ฒญ์‹œ ํ—ค๋” Content-Type์„ text/plain์œผ๋กœ ์„ค์ •ํ–ˆ์„ ๋•Œ์™€ ์•„๋‹ ๋•Œ (ํ”„๋ฆฌํ”Œ๋ผ์ดํŠธ ํ™•์ธ์šฉ) ๋“ฑ ์ด๋Ÿฐ ์ €๋Ÿฐ ์กฐํ•ฉ์œผ๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด๊ณ  ๋„คํŠธ์›Œํฌ์ฐฝ์„ ํ™•์ธํ•ด๋ดค๋‹ค. ํฌํŠธ๊ฐ€ ๋™์ผํ•  ๋•Œ๋Š” ERR_FAILED ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ , ํฌํŠธ๊ฐ€ ๋‹ค๋ฅผ ๋•Œ๋Š” ERR_EMPTY_RESPONSE ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”๋ฐ, ์ „์ž๋Š” ๋ธŒ๋ผ์šฐ์ € ๋ฌธ์ œ์ธ ๊ฒƒ ๊ฐ™๊ณ  ํ›„์ž๋Š” ์˜๋„๋Œ€๋กœ ๋‚˜์˜จ CORS์ •์ฑ… ์œ„๋ฐ˜์œผ๋กœ์ธํ•œ ์—๋Ÿฌ์ธ ๊ฒƒ ๊ฐ™์•˜๋‹ค. 

 

๐ŸŽ ๊ธฐ์–ตํ•  ๊ฒƒ
โœ”๏ธ Simple Request ์กฐ๊ฑด
์กฐ๊ฑด 1. ๋ฉ”์„œ๋“œ : GET, POST, HEAD
์กฐ๊ฑด 2. ํ—ค๋” : Accept, Accept-Language, Content-Language, Content-Type, DPR, Downlink, Save-Data, Viewport-Width, Width
์กฐ๊ฑด 3. Content-type ๊ฐ’ : application/x-www-form-urlencoded, multipart/form-data, text/plain
(์ด ์ค‘ ํ•˜๋‚˜๋ผ๋„ ๋งŒ์กฑํ•˜์ง€ ์•Š์œผ๋ฉด Preflight ์š”์ฒญ์„ ๋‚ ๋ ค๋ฒ„๋ฆผ)

 

๐ŸŽ ๋” ๊ณต๋ถ€ํ•  ๊ฒƒ
โœ”๏ธ ERR_FAILED, ERR_EMPTY_RESPONSE
โœ”๏ธ ํ† ์ด ๋ณต์Šต
๋ฐ˜์‘ํ˜•

'Daily > Today I Learned' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

210208_TIL  (0) 2021.02.09
210207_TIL  (0) 2021.02.08
210205_TIL  (0) 2021.02.06
210204_TIL  (0) 2021.02.05
210203_TIL  (0) 2021.02.04

๋Œ“๊ธ€