.then, .catch
<script>
var fetched = fetch ('http://jsonplaceholder.typicode.com/posts') // 특정 url에서 값을 가져옴
fetched.then (function(response) { // url에서 값을 가져오는 것이 성공할 경우 실행할 콜백함수
console.log("response: ", response);
} );
fetched.catch (function(reason) { // url에서 값을 가져오는데 실패하거나 에러 발생시 실행할 콜백함수
console.log("reason: ", reason);
} );
</script>
간소화
<script>
fetch ('http://jsonplaceholder.typicode.com/posts'); // 특정 url에서 값을 가져옴
.then (function(response) { // url에서 값을 가져오는 것이 성공할 경우 실행할 콜백함수
console.log("response: ", response);
} );
.catch (function(reason) { // url에서 값을 가져오는데 실패하거나 에러 발생시 실행할 콜백함수
console.log("reason: ", reason);
} );
</script>
더 간소화
<script>
fetch ('http://jsonplaceholder.typicode.com/posts'); // 특정 url에서 값을 가져옴
.then (function(response) { // url에서 값을 가져오는 것이 성공할 경우 실행할 콜백함수
console.log("response: ", response);
} );
.catch (function(reason) { // url에서 값을 가져오는데 실패하거나 에러 발생시 실행할 콜백함수
console.log("reason: ", reason);
} );
</script>
response의 속성
response.text(), response.json(), 기타등등
응답받은 데이터를 text 타입이나 json으로 가공
'프로젝트 > 파이널 프로젝트' 카테고리의 다른 글
프로젝트 17일차 / fetch put 회원정보수정 시 백으로 데이터가 넘어오지 않는 현상 (ajax로 해결) (0) | 2022.12.07 |
---|---|
프로젝트 15일차 (0) | 2022.12.05 |
프로젝트 12일차 / 자바스크립트 checkbox 체크 유무에 따른 비동기 페이지 이동 (0) | 2022.12.02 |
프로젝트 12일차 / 자바스크립트 append 문제 해결 (0) | 2022.12.02 |
프로젝트 10일차 (0) | 2022.11.30 |