๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐ŸŒ  ์ž๋ฐ” ์„œ๋ฒ„ ํŽ˜์ด์ง€ JSP/๐ŸŒž ์—์ด์ž‘์Šค Ajax

Ajax - Ajax(์—์ด์ž‘์Šค)๋ž€?

by Meteora_ 2021. 3. 15.
728x90

 1. Ajax๋ž€? 

Ajax๋Š” JavaScript์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ค‘ ํ•˜๋‚˜์ด๋ฉฐ 

Asynchronous Javascript And Xml(๋น„๋™๊ธฐ์‹ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์™€ xml)์˜ ์•ฝ์ž์ด๋‹ค.

๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๊ฐ€์ง€๊ณ ์žˆ๋Š” XMLHttpRequest ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด์„œ ์ „์ฒด ํŽ˜์ด์ง€๋ฅผ ์ƒˆ๋กœ ๊ณ ์น˜์ง€ ์•Š๊ณ ๋„

ํŽ˜์ด์ง€์˜ ์ผ๋ถ€๋งŒ์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•˜๋Š” ๊ธฐ๋ฒ• ์ด๋ฉฐ

 Ajax๋ฅผ ํ•œ๋งˆ๋””๋กœ ์ •์˜ํ•˜์ž๋ฉด JavaScript๋ฅผ ์‚ฌ์šฉํ•œ ๋น„๋™๊ธฐ ํ†ต์‹ ,

ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„๊ฐ„์— XML ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›๋Š” ๊ธฐ์ˆ ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

Ajax๋Š” ํ˜„์žฌ ํ™”๋ฉด์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋Œ์–ด์˜ค๋Š” ๊ฒƒ์ด๋‹ค.

 

			url:"data.jsp",
			type:"get", //get/post ->servlet
			//data: "t1=XYZ&t2=์•ˆ๋…•ํ•˜์„ธ์š”",
			data: {t1:"๊ฐ•์•„์ง€", t2:"๊ณ ์–‘์ด"},

 

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ajax ์˜ˆ์‹œ</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>ํด๋ฆญ</button>



<script type="text/javascript">
$(function () {
	
	$("button").click(function () {
		
		
		//๊ธฐ๋ณธ๋ฌธ๋ฒ•
		$.ajax({
			
			//๊ฐˆ๋–„ ํ•„์š”ํ•œ๊ฒƒ,
			//์ด๋™ํ• ๋•Œsend////////////
			url:"data.jsp",
			type:"get", //get/post ->servlet
			//data: "t1=XYZ&t2=์•ˆ๋…•ํ•˜์„ธ์š”",
			data: {t1:"๊ฐ•์•„์ง€", t2:"๊ณ ์–‘์ด"},
			
			
			////////////////////////send
			
			///////////////////////๊ฐ”๋‹ค์™”์„๋–„ 
			//recv
			
			//function ๊ฐ”๋‹ค์™€์„œ ๋ฐ์ดํƒ€๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์˜  data
			success: function(data, status, xhr){
				//alert("suc");
				//๊ฐ”๋‹ค์™”์„๋–„ ์„ฑ๊ณตํ•˜๋ฉด ๋œฌ๋‹ค.
			
				//alert(data);  //data.jsp ์˜ ํŒŒ์ผ ์ฝ”๋“œ๋ฅผ ๋‹ค ๊ฐ€์ ธ์˜จ๋‹ค.
				$("#demo").html(data);
				//alert(status);
				//alert(xhr);
				alert(xhr.responseText);
			},
			error:function(/* xhr, status, error */){
				alert("error");
			},
			complete:function(xhr, status){
				alert("ํ†ต์‹ ์ข…๋ฃŒ");	
			}
			/////////////////////////recv
			
		});
		
		
		
	});
	
});


</script>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
t1 = <%=request.getParameter("t1") %>
<br>
t2 = <%=request.getParameter("t2") %>

๋Œ“๊ธ€