


지금 구조상 함수의 리턴값으로 응답 데이터를 받는게 불가능 하다.
- 함수 호출을 브라우저가 하기 때문에
- onload 에서 데이터를 즉각 소비 해야 된다.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 비동기 get요청 처리 함수
function get(url) {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.send();
function getRespData(e) {
// 서버 응답 데이터를 리턴
console.log('server response: ',xhr.response);
return JSON.parse(xhr.response);
};
xhr.onload = getRespData;
}
(function() {
const foundUser = get('http://localhost:5000/user/1');
const boardList = get('http://localhost:5000/board');
console.log('user:', foundUser);
console.log('board:', boardList);
})();
</script>
</body>
</html>
'Spring' 카테고리의 다른 글
[Spring / 비동기 #11] 플로저와 Promise_22.07.29 [13일차] (0) | 2022.07.29 |
---|---|
[Spring / 비동기 #10] ajax의 문제 해결(콜백지옥)_22.07.29 [13일차] (0) | 2022.07.29 |
[Spring / 비동기 #8] RESTful_scoreApp 실습_22.07.29 [13일차] (0) | 2022.07.29 |
[Spring / 비동기 #7] js(npm서버) 비동기 데이터처리 실습_22.07.28 [12일차] (0) | 2022.07.28 |
[Spring / 비동기 #6] js_npm서버 사용법 / ajax _22.07.28 [12일차] (0) | 2022.07.28 |