본문 바로가기

개발/JavaScript

[Ajax] #03. Ajax 예제 :: (2) Click시 jsp -> html -> jsp 데이터 갔다가 돌아오기

● Click시 jsp -> html -> jsp 데이터 갔다가 돌아오기

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 
t1=<%=request.getParameter("t1"%>
t2=<%=request.getParameter("t2"%>
 
</body>
</html>
cs

 

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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
 
<p id="demo"></p>
<br>
<button type="button"> click </button>
 
<script type="text/javascript">
 
/* 완전한 ajax를 만들어보자.*/
$(function () {
    
    $("button").on("click"function () {
        
        $.ajax({
            //////////////////////////////////////////////// Send data
            url: "data.jsp",
            type: "get",                             // Servlet(doGet, doPost) 가는것!
                                                    // Ajax는 보안에 좋은 편이다. Parameter에 안 나타난다. 
        //    data: "t1=XYZ&t2=자차카",                // (1) 같은 처리
            data: {t1:"XYZ", t2: "자차카" },        // (2) 
 
            /////////////////////////////////////////////////// receive(받음), 심부름 갔다가 돌아온다
            success:function(data, status, xhr) {
            //    alert("success");
            },
            error: function(xhr, status, error) {
                alert("error");
            },
            complete: function(xhr, status) {
                alert("통신 종료");
            }
                                                
        });
 
    });
    
});
 
</script>
 
</body>
</html>
cs

 

 

Ajax 성공! 

t1=<%=request.getParameter("t1") %>
t2=<%=request.getParameter("t2") %>

 

 

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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
 
<p id="demo"></p>
<br>
<button type="button"> click </button>
 
<script type="text/javascript">
 
/* 완전한 ajax를 만들어보자.*/
$(function () {
    
    $("button").on("click"function () {
        
        $.ajax({
            //////////////////////////////////////////////// Send data
            url: "data.jsp",
            type: "get",                         // Servlet(doGet, doPost) 가는것!
                                                    // Ajax는 보안에 좋은 편이다. Parameter에 안 나타난다. 
        //    data: "t1=XYZ&t2=자차카",                    // (1) 같은 처리
            data: {t1:"XYZ", t2: "자차카" },            // (2) 
 
            /////////////////////////////////////////////////// receive(받음), 심부름 갔다가 돌아온다
            success:function(data, status, xhr) {
 
                $("#demo").html(data);
                alert(status);            // 결과 : Success 
                alert(xhr);             // 결과 : object Object  
            },
            error: function(xhr, status, error) {
                alert("error");
            },
            complete: function(xhr, status) {
                alert("통신 종료");
            }
                                                    
        });
    });
});
 
 
</script>
 
</body>
</html>
cs

※ 자바스크립트에선 Ajax 통신을 위해 XMLHttpRequest 객체를 사용한다.

XHR은 XMLHttpRequest의 약자라고 보면 된다.