JavaScript / ajax 복습

권단비·2023년 5월 22일
0

IT

목록 보기
138/139
<!DOCTYPE html>
<html lang="en">
    <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>
        <h2>This is a heading</h2>

        <p>This is a paragraph.</p>
        <p>This is another paragraph.</p>

        <button>Click me</button>
    </body>
</html>


[aJax]

ajax = 비동기 통신 | jquery 객체 안에 있는 함수
⇒새로고침 없이 반영

<!DOCTYPE html>
<html lang="en">
    <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>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $("button").click(function () {
                $("p").hide();
            });
        });
    </script>

    <script>
        $(document).ready(function () {
            $.ajax({
                type: "GET",
                url: "http://sample.bmaster.kro.kr/contacts?pageno=3&pagesize=10", // json으로 받음
                success: function (result) {
                    console.log(result);
                    $(result.contacts).each(function(){
                        document.write("<h1>" + this.no + "<h1>");
                            document.write("<h1>" + this.name + "<h1>");
                    });
                },
                error: function (e) {
                    console(e);
                },
            });
        });
    </script>
    <body>
        <h2>This is a heading</h2>

        <p>This is a paragraph.</p>
        <p>This is another paragraph.</p>

        <button>Click me</button>
    </body>
</html>

0개의 댓글