Programming/javascript

[자바스크립트] 비동기 통신axios

멍쟈뽀쨕 2024. 12. 18. 11:15

 
비동기 통신은 여러가지가 있다 AJAX, FETCH, AXIOS등등
아래는 AXIOS 기본 폼 예시이다.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script>
        const getProductList = async () => {
            await axios
                .get('[여기 요청 받을 url]')
                .then((response) => {
                    console.log(response.data); // 응답 데이터
                    return response.data;
                })
                .catch((error) => {
                    console.error('Error fetching data:', error);
                });
        }
        const response = getProductList();
        console.log(response);
    </script>
</body>

</html>