jQuery | javascript | CSS

67.jQuery - $.ajaxSetup()으로 $.ajax() 단순화 ( 09.Ajax )

Godffs 2009. 11. 17. 10:37
반응형
$.ajaxSetup()
- 한 페이지에서 동일하게 반복되는 $.ajax() 메서드에 동일한 설정을 유지
  : $.ajax()를 여러 번 사용시 반복되는 인자 생략 가능
- $.ajax()에서 사용하는 인자를 그대로 사용

12.jQuery.ajajxSetup.htm

<head>

    <title>반복사용하는 ajax() 메서드의 주요 속성을 ajaxSetup()에서 설정</title>

    <style type="text/css">

        .red{ background-color:Red;}

        .yellow{background-color:Yellow;}

    </style>

   

    <script src="../jQuery/jquery-1.3.2-vsdoc2.js" type="text/javascript"></script>

    <script type="text/javascript">

        $(document).ready(function() {

        //$.ajax()에서 반복 사용하는 속성을 상단에 설정 $.ajax에서 상속 받아 사용

        $.ajaxSetup({

                url: "02.jQuery.getJSON.js", //a.aspx, a.php, a.jsp

                dataType: "json"

            });

           

            $.ajax({

                success: function(data) {                   

                    $.each(data, function() {

                        $('#tblList').append("<tr><td>" + this.Num + "</td><td>"

                                 + this["Name"] + "</td></tr>");

                    });

                   

                    //홀수 짝수 번호에 색깔 넣기

                    $('table:first tr:odd').addClass('yellow');

                    $('table:first tr:even').addClass('red');

                }

            });

           

            $.ajax({

                success: function(data) {                   

                    $.each(data, function() {

                        $('#tblList2').append("<tr><td>" + this.Num + "</td><td>"

                                 + this["Name"] + "</td></tr>");

                    });

                    $('table:last tr:odd').addClass('yellow');

                    $('table:last tr:even').addClass('red');

                }

            });

        });

    </script>

</head>

<body>

        <table id="tblList" border="1"></table>

        <table id="tblList2" border="1"></table>       

</body>

</html>


결과화면

[그림67-1]



반응형