Programming/React

[ React ] GET ํ˜ธ์ถœํ•˜์ง€๋„ ์•Š์•˜๋Š”๋ฐ ๊ณ„์† GET ํ˜ธ์ถœ๋˜๋Š” ํ˜„์ƒ

seulye 2024. 8. 8. 11:08

 

  const onClickLogin = () => {
    axios
      .post(`/api/comm/user/signin`, {
        id: id,
        password: pw,
      })
      .then((response) => {
        console.log(response.data);
      })
      .catch((error) => {
        console.log(error);
      });
  };

๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ–ˆ๋Š”๋ฐ, ๊ณ„์† get์ด ์ฐํžŒ๋‹ค.

 

๋ฌผ์Œํ‘œ๊ฐ€ ๋ถ™๋Š” ์ด์œ ๋Š”,

HTML ํผ์ด GET ๋ฉ”์†Œ๋“œ๋กœ ์ œ์ถœ๋˜๋ฉด, ํผ ํ•„๋“œ ๊ฐ’์ด ์ž๋™์œผ๋กœ ์ฟผ๋ฆฌ ์ŠคํŠธ๋ง์œผ๋กœ ์ถ”๊ฐ€๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

const onClickLogin = (event) => {
    event.preventDefault();
    axios.post(`/api/comm/user/signin`,{
      id:id,
      password:pw
    }) 
    .then((response) => {
      console.log(response.data)
    })
    .catch((error) => {
      console.log(error);
    });
  }

 

๊ทธ๋ž˜์„œ event.preventDefault(); ์ด๋ถ€๋ถ„ ์ถ”๊ฐ€. ์–ด๋–ค ์ด๋ฒคํŠธ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, ํ•ด๋‹น ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ์‚ฌ์šฉ์ž ์—์ด์ „ํŠธ์˜ ๊ธฐ๋ณธ ๋™์ž‘์„ ์‹คํ–‰ํ•˜์ง€ ์•Š๋„๋ก ์ง€์ •

 

https://developer.mozilla.org/ko/docs/Web/API/Event/preventDefault

 

Event.preventDefault() - Web API | MDN

Event ์ธํ„ฐํŽ˜์ด์Šค์˜ preventDefault() ๋ฉ”์„œ๋“œ๋Š” ์–ด๋–ค ์ด๋ฒคํŠธ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, ํ•ด๋‹น ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ์‚ฌ์šฉ์ž ์—์ด์ „ํŠธ์˜ ๊ธฐ๋ณธ ๋™์ž‘์„ ์‹คํ–‰ํ•˜์ง€ ์•Š๋„๋ก ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

developer.mozilla.org