본문 바로가기
Frontend/Ajax

동기와 비동기식 결과 차이

by 지구 2018. 5. 23.

동기와 비동기의 차이를 request.readyState 메소드를 통하여 확인해보자.

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<!DOCTYPE html>
<html>
 
<head>
    <meta charset="EUC-KR">
    
    <script type="text/javascript">
        var request = null;
        
        function createRequest(){
            try{
                request = new XMLHttpRequest();
                //Debug..
                //alert ("XMLHttpRequest() 로 request instance생성완료");
            }catch(trymicrosoft){
                try{
                    request = new ActiveXObject("Msxml2.XMLHTTP");
                    //Debug..
                    //alert ("ActiveXObject(Msxml2.XMLHTTP) 로 request instancet생성완료");
                }catch(othermicrosoft){
                    try{
                        request =new ActiveXObject("Microsoft.XMLHTTP");
                        //Debug..
                        //alert ("new ActiveXObject(Microsoft.XMLHTTP) 로 request instancet생성완료");
                    }catch(failed){
                        request = null;
                    }
                }
            }
            
            if( request == null ){
                alert ("request객체 생성시 error 발생함.");
            }            
        }
        
        function getSold(){
            createRequest();
            
            var url = "calcServerAjax.jsp";
            
            request.open("GET",url,true); //비동기
            //request.open("GET",url,false); //동기
            
            request.onreadystatechange = updatePage;
            
            console.log("==> 1. request.readyState : "+request.readyState);
            request.send(null);
            console.log("==> 2. request.readyState : "+request.readyState);
            console.log("==> 3. request.readyState : "+request.readyState);
            console.log("==> 4. request.readyState : "+request.readyState);
        }
        
        function updatePage(){
            console.log("====> updatePage() : "+request.readyState);
        }
    </script>
</head>
 
<body>
 
    <h1> 판매 보고서</h1>
 
    <table>
        <tr>
            <th>판매된 상품 갯수</th>
            <td><span id="sold">100</span></td>
        </tr>
        <tr>
            <th>상품 단가</th>
            <td><span id="price">100</span>원</td>
        </tr>
    </table>
    
    <h2>총 매출금액  : <span id="cash">100,00원</span></h2>
    
    <form  method="POST" action="calcServerNormal.jsp">
        <input type="submit" value="판매보고서 다시보기(기존방식)">
    </form>
    
    <form>
        <input type="button" value="판매보고서 다시보기(ajax적용)"  onClick="javascript:getSold()">
    </form>
 
</body>
 
</html>
cs

 

 

반응형

'Frontend > Ajax' 카테고리의 다른 글

[JavaScript] Ajax Error Callback 에서 this.~ 호출 주의  (0) 2019.10.02
jsp 에서 ajax 통신시 한글깨짐  (0) 2018.07.27
Ajax 코딩 절차  (0) 2018.05.17
Ajax 기본개념  (0) 2018.05.17

댓글