๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์›น๊ณต๋ทฐ

js fetch ํ›„, response ์‘๋‹ต์ด ๋Š๋ฆฐ ๋ฌธ์ œ

by ์ด๋…ธํ‚ค_ 2022. 4. 29.

๋ฌธ์ œ

let url = `${TSP_SVR_URL}/fix_points`;
    fetch(url, {
        method: "POST",
        headers: {
            "Content-type": "applicaion/json",
        },
        body: JSON.stringify({
            SPoint: pointList[0],
            EPoint: pointList[0],
            SPointList:{
                nodes:pointList.slice(1, cnt)
            },
        }),
    })
    .then(response => response.json())
    .then(body => {
        console.log(body)
    });

fetch ์š”์ฒญ ์‹œ์— response์˜ body๊ฐ€ ๋„ˆ๋ฌด ๋Šฆ๊ฒŒ ์˜ค๋Š” ํ˜„์ƒ์ด์—ˆ๋‹ค. 

ํ”„๋ฆฌํ”Œ๋ผ์ดํŠธ๊ฐ€ ์ œ์ผ ๋จผ์ € ์˜ค๊ณ , ๊ทธ๋‹ค์Œ fetch response๊ฐ€ ๋“ค์–ด์˜ค๋Š”๋ฐ ์‹œ๊ฐ„์ด ๋ฌด๋ ค 5.14์ดˆ์ด๋‹ค. 

๋‹จ์ˆœ string๋งŒ ๋ฐ›๋Š”๋ฐ๋„ ๋ง์ด๋‹ค. 

 

preflight๊ฐ€ ๋ญ”์ง€ํ•ด์„œ ์ฐพ์•„๋ณด๋‹ค๊ฐ€ ์•„๋ž˜ ๋ธ”๋กœ๊ทธ๋ฅผ ๋ณด๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ "๊ฐ„๋‹จํ•œ ์š”์ฒญ"์„ ๋ณด๋‚ด๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด๋“ค์ด ์„ค๋ช…๋˜์–ด์žˆ๋‹ค. 

CORS ์˜๋ฏธ / ํ˜„์—…์—์„œ ์ง์ ‘ ๊ฒช์€ CORS ์ด์Šˆ์™€ ํ•ด๊ฒฐ๋ฐฉ๋ฒ• ์ •๋ฆฌ (tistory.com)

 

CORS ์˜๋ฏธ / ํ˜„์—…์—์„œ ์ง์ ‘ ๊ฒช์€ CORS ์ด์Šˆ์™€ ํ•ด๊ฒฐ๋ฐฉ๋ฒ• ์ •๋ฆฌ

์ž…์‚ฌ ์ดˆ๊ธฐ์— ์ž˜ ๋ชฐ๋ผ์„œ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐ ์˜ค๋ž˜ ๊ฑธ๋ ธ๋˜ ์ด์Šˆ์˜€๋‹ค. ์‚ฌ์‹ค ์ง€๊ธˆ ์ƒ๊ฐํ•˜๋ฉด ํ•ด๊ฒฐ์ ์ด ๊ฝค๋‚˜ ๋šœ๋ ทํ•œ ์ดˆ๋ณด์ ์ธ ๊ณ ๋ฏผ์ด์—ˆ๋Š”๋ฐ ๋‹น์‹œ์—๋Š” ๋Œ€์ฒด ๋ˆ„๊ฐ€ ์ด๊ฑธ ํ•ด๊ฒฐํ•ด์•ผ ํ•˜๋‚˜ ๊ตฌ๊ธ€๋งŒ ๋’ค์ง€๊ณ  ์žˆ์—ˆ๋‹ค. pre

eunjinii.tistory.com

๊ทธ๋ž˜์„œ fetch์‹œ์— content-type์„ text/plain์œผ๋กœ ๋ณ€๊ฒฝํ•œ ํ›„์—๋Š” ์‘๋‹ต์†๋„๊ฐ€ ์ •์ƒ์œผ๋กœ ๋Œ์•„์˜ค๊ณ  preflight ์š”์ฒญ์€ ๋”์ด์ƒ ๋‚˜๊ฐ€์ง€ ์•Š๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

let url = `${TSP_SVR_URL}/fix_points`;
    fetch(url, {
        method: "POST",
        headers: {
            "Content-type": "text/plain"
        },
        body: JSON.stringify({
            SPoint: pointList[0],
            EPoint: pointList[0],
            SPointList:{
                nodes:pointList.slice(1, cnt)
            },
        }),
    })
    .then(response => response.json())
    .then(body => {
        console.log(body)
    });

๋Œ“๊ธ€