React/리액트(코딩애플)

ajax / aixos / catch

오류확인자 2023. 6. 24. 14:19

ajax 서버

서버에 데이터를 요청

서버 : 부탁하면 진짜로 들어주는 프로그램

ajax란

서버에 get, post 요청할 때 새로고침 없이 데이터를 주고 받을 수 있게 도와주는 간단한 브라우저 기능이다.

ajax 사용해도 GET 요청 가능

ajax 이용한 GET요청은 axios.get(’url’)

<button onClick={() => {
              axios.get('')
            }}>버튼</button>
          </>

ajax 요쳥결과는 ajax.get(’url’).then()

서버가 보낸 실제 핵심 데이터 보려면 console.log(결과.data)

 {
              axios.get('<https://codingapple1.github.io/shop/data2.json>')
                .then((결과) => { 
                  console.log(결과.data)
                })

실제 결과

 

catch

ajax 요청이 실패할 경우 catch로 이용해서 실패하면 띄울 코드

 {
              axios.get('<https://codingapple1.github.io/shop/data2.json>')
                .then((결과) => {
                  console.log(결과.data) // 실제 데이터 
                })

                .catch(() => {
                  console.log('실패함 ㅅㄱ')
                })