본문 바로가기
DEVELOPMENT

Fetch API

by Z@__ 2021. 3. 2.
반응형

www.youtube.com/watch?v=cuEtnrL9-H0&ab_channel=Academind

 

 

reqres.in/api/users

 

 

 

 

console.log(fetch('https://reqres.in/api/users'))

 

 

 

 

 

 

fetch('https://reqres.in/api/users')
	.then(res => console.log(res))

 

 

 

fetch('https://reqres.in/api/users')
    .then(res => res.json())
    .then(data => console.log(data))

 

 

 

 

fetch('https://reqres.in/api/users/23')
    .then(res => res.json())
    .then(data => console.log(data))

 

 

 

 

 

fetchAPI catch 처리

 

fetch('https://reqres.in/api/users/23')
    .then(res => res.json())
    .then(data => console.log(data))
    .catch(error => console.log('ERROR'))

 

 

 

fetch('https://reqres.in/api/users/23')
    .then(res => {
        if(res.ok) {
            console.log('SUCCESS')
        } else {
            console.log('Not Successful')
        }
    })
    .then(data => console.log(data))
    .catch(error => console.log('ERROR'))

 

 

 

 

fetch('https://reqres.in/api/users/')
    .then(res => {
        if(res.ok) {
            console.log('SUCCESS')
        } else {
            console.log('Not Successful')
        }
    })
    .then(data => console.log(data))
    .catch(error => console.log('ERROR'))

 

 

 

fetch('https://reqres.in/api/users/', {
    method: 'POST',
    body: {
        name: 'User 1'
    }
}).then(res => {
        return res.json()
})
.then(data => console.log(data))
.catch(error => console.log('ERROR'))

 

 

 

 

 

fetch('https://reqres.in/api/users/', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        name: 'User 1'
    })
}).then(res => {
        return res.json()
})
.then(data => console.log(data))
.catch(error => console.log('ERROR'))

반응형

댓글